By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,576 Members | 1,399 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,576 IT Pros & Developers. It's quick & easy.

remove radio button

P: n/a
Hi,

I have a radio button group with 2 elements. Can I use javascript to
remove the last element based on user's input? Thanks.

Dec 4 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a


Well:

function popLast() {
myRadios=document.YOURFORM.DUCKS;
myRadios[myRadios.length-1]=null;
}
-------------------------------------------------------
<form name="YOURFORM">
<input type="radio" name="DUCKS" value="1">
<input type="radio" name="DUCKS" value="2">
</form>

<a href="#" onclick="popLast();return false"> pop last </a>

Danny
Dec 4 '05 #2

P: n/a
Danny wrote:

Well:

function popLast() {
myRadios=document.YOURFORM.DUCKS;
myRadios[myRadios.length-1]=null;
}

Did you test that? I guess not.

There is no need to create a global variable.

When accessing form elements using dot notation, you may get back a
single element or a collection. A single element won't have a
'length' property so your script will throw an error when there is
only one radio button is left in the group.

DOM 2 specifies that the length property of a HTML collection is
readonly, setting it to some value has no effect in conforming browsers.

function popLast() {
var myRadios=document.YOURFORM.DUCKS;
if (myRadios.length && document.removeChild){
var lastOne = myRadios[myRadios.length-1];
lastOne.parentNode.removeChild(lastOne);
}
}
A radio group with only one element doesn't make much sense, since one
button should always be selected.

-------------------------------------------------------
<form name="YOURFORM">
<input type="radio" name="DUCKS" value="1">
<input type="radio" name="DUCKS" value="2">
</form>

<a href="#" onclick="popLast();return false"> pop last </a>


The error of using an A element that way has been discussed at length
recently, it should not be done even in an example. Why no use a button?

<input type="button" value="Pop last" onclick="popLast();">

--
Rob
Dec 4 '05 #3

P: n/a
RobG wrote on 04 dec 2005 in comp.lang.javascript:
function popLast() {
var myRadios=document.YOURFORM.DUCKS;
if (myRadios.length && document.removeChild){
if (myRadios && myRadios.length && document.removeChild){

otherwise you will get an error when there are
no more Agatha's little niggers [indians for the osotp]
var lastOne = myRadios[myRadios.length-1];
lastOne.parentNode.removeChild(lastOne);
}
}


--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Dec 4 '05 #4

P: n/a
Danny wrote:
function popLast() {
myRadios=document.YOURFORM.DUCKS;
myRadios[myRadios.length-1]=null;
}
-------------------------------------------------------
<form name="YOURFORM">
<input type="radio" name="DUCKS" value="1">
<input type="radio" name="DUCKS" value="2">
</form>

<a href="#" onclick="popLast();return false"> pop last </a>


Of course this does not work.[1] If it were that simple,
no removeChild/removeNode method would be necessary.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Script-Type" content="text/javascript">
...
<title>...</title>
</head>

<body>
...
<form action="">
<fieldset>
<legend></legend>
<script type="text/javascript">
function isMethodType(s)
{
return (s == "function" || s == "object");
}

function popLast(o)
{
var f, es;
if ((f = o.form) && (es = f.elements))
{
var myRadios = es['ducks'];
if (myRadios)
{
var r = (myRadios.length
? myRadios[myRadios.length - 1]
: myRadios);

// W3C DOM Level 2+
if (r.parentNode
&& isMethodType(typeof r.parentNode.removeChild))
{
r.parentNode.removeChild(r);
}

// IE DOM (untested)
else if (isMethodType(typeof r.removeNode))
{
r.removeNode();
}
}
}
}

document.write(
'<input type="button" value="Pop Last"'
+ ' onclick="popLast(this);">');
</script>
<input type="radio" name="ducks" value="1">
<input type="radio" name="ducks" value="2">
</fieldset>
</form>
...
<body>
</html>
PointedEars

P.S.
Please quote what you are replying to:
<URL:http://jibbering.com/faq/faq_notes/pots1.html>
___________
[1] Tested in
"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.12) Gecko/20050922
Firefox/1.0.7 (Debian package 1.0.7-1) Mnenhy/0.7.2.0"
Dec 4 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.