...

View Full Version : Bug in JS ???



phantom007
05-08-2008, 01:45 PM
Hi

Plz consider the following JS code. This code will alert you with the value of the radio button that is selected. This code works perfectly fine..


<script>
function deletecID(){
for (i=0;i<document.frmlist.intDeleteValue.length;i++){
if (document.frmlist.intDeleteValue[i].checked==true){
var deleteID =document.frmlist.intDeleteValue[i].value
alert(deleteID);
}
}
}

</script>

<form name="frmlist">
<input type=radio name="intDeleteValue" value="1">
<input type=radio name="intDeleteValue" value="2">
<input type=button onclick="deletecID()">
</form>


Now, take a look into the following code. This code does not work. Its the same code as above but the only difference is that it just have one radio button in the form.



<script>
function deletecID(){
for (i=0;i<document.frmlist.intDeleteValue.length;i++){
if (document.frmlist.intDeleteValue[i].checked==true){
var deleteID =document.frmlist.intDeleteValue[i].value
alert(deleteID);
}
}
}

</script>

<form name="frmlist">
<input type=radio name="intDeleteValue" value="1">
<input type=button onclick="deletecID()">
</form>


Any JS gurus here can tell if this is a bug in JS? Is there a solution to this?

Thanx

tagnu
05-08-2008, 02:00 PM
This works


<script>
function deletecID(){

if (document.frmlist.intDeleteValue.checked==true){
var deleteID =document.frmlist.intDeleteValue.value
alert(deleteID);

}
}

</script>

<form name="frmlist">
<input type=radio name="intDeleteValue" value="1">
<input type=button onclick="deletecID()">
</form>

If you have only one element to check use the document.getElementById() (http://developer.mozilla.org/en/docs/DOM:document.getElementById) method.

phantom007
05-08-2008, 02:04 PM
True but this does not


<script>
function deletecID(){

if (document.frmlist.intDeleteValue.checked==true){
var deleteID =document.frmlist.intDeleteValue.value
alert(deleteID);

}
}

</script>

<form name="frmlist">
<input type=radio name="intDeleteValue" value="1">
<input type=radio name="intDeleteValue" value="2">
<input type=radio name="intDeleteValue" value="3">
<input type=button onclick="deletecID()">
</form>



Could you please tell me why is the code on my first post does not work and wots the solution? is it a bug?

Philip M
05-08-2008, 02:11 PM
I don't think it is a bug - radio buttons are supposed to form a group, i.e. more than one selection.


<script>
function deletecID(){
for (var i = 0; i <document.frmlist.intDeleteValue.length; i++) {
if (document.frmlist.intDeleteValue[i].checked == true){
var deleteID =document.frmlist.intDeleteValue[i].value
alert(deleteID);
}
}
}

</script>

<form name="frmlist">
<input type=radio name="intDeleteValue" value="1">
<input type=radio name="intDeleteValue" value="2">
<input type=radio name="intDeleteValue" value="3">
<input type=button onclick="deletecID()">
</form>

phantom007
05-08-2008, 02:17 PM
See, I have a list of details on my site, I should be able to delete items that I have selected using radiobuttons at the click of a button.

The problem arises when there is only one item left in the list. I should be able to delete that also but unfortunately this does not work.

Can you suggest something?

Kor
05-08-2008, 02:27 PM
Use getElementsByName() method.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
function deletecID(){
var rad=document.getElementsByName('intDeleteValue'), i=0, r;
while(r=rad[i++]){
r.checked?alert(r.value):null;
}
}
</script>
</head>
<body>
<form action="">
<input type="radio" name="intDeleteValue" value="1">
<input type="button" onclick="deletecID()">
</form>
</body>
</html>

phantom007
05-08-2008, 02:32 PM
Thanx Kor, your code works fine.

So tell me was it a bug that I discovered?

Kor
05-08-2008, 03:16 PM
No, it is not a bug. ECMA basic syntax considers that a collection (array) is formed only when there are 2 or more elements with the same name.

Eventually, DOM 2 came with a more abstract view of the document's elements' reference, so that its new method: getElementsByName() is able to set a collection of any form's elements with the same name, no matter the number.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum