Bug in JS ???

Post May 8th, 2008, 5:08 am

Hi

please 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..

  1. <script>
  2. function deletecID(){
  3.         for (i=0;i<document.frmlist.intDeleteValue.length;i++){
  4.         if (document.frmlist.intDeleteValue[i].checked==true){
  5.         var deleteID =document.frmlist.intDeleteValue[i].value
  6.         alert(deleteID);
  7.         }
  8.         }
  9. }
  10.  
  11. </script>
  12.  
  13. <form name="frmlist">
  14. <input type=radio name="intDeleteValue" value="1">
  15. <input type=radio name="intDeleteValue" value="2">
  16. <input type=button  onclick="deletecID()">
  17. </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.


  1. <script>
  2. function deletecID(){
  3.         for (i=0;i<document.frmlist.intDeleteValue.length;i++){
  4.         if (document.frmlist.intDeleteValue[i].checked==true){
  5.         var deleteID =document.frmlist.intDeleteValue[i].value
  6.         alert(deleteID);
  7.         }
  8.         }
  9. }
  10.  
  11. </script>
  12.  
  13. <form name="frmlist">
  14. <input type=radio name="intDeleteValue" value="1">
  15. <input type=button  onclick="deletecID()">
  16. </form>



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

Thanx
Interview Questions & Answers - http://www.focusinterview.com
Cheapest Hosting - http://www.igchosting.in
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post May 8th, 2008, 5:08 am

  • dimat
  • Student
  • Student
  • User avatar
  • Joined: 02 Aug 2006
  • Posts: 67
  • Loc: Kiev, Ukraine
  • Status: Offline

Post May 8th, 2008, 7:44 am

in first example document.frmlist.intDeleteValue - is an array (NodeList) but in second example it is just a htmlRadioButton, a single object.

use this:
  1.  
  2. <script>
  3.    function deletecID(){
  4.         elems = document.getElementsByTagName("input");
  5.         for(i=0; i<elems.length; ++i)
  6.             if(elems[i].name="intDeleteValue"&&elems[i].checked==true) alert(elems[i].value);
  7.    }
  8.    
  9.    </script>
  10.    
  11.    <form name="frmlist">
  12.     <input type=radio name="intDeleteValue" value="2"/>
  13.     <input type=radio name="intDeleteValue" value="1"/>
  14.     <input type=radio name="intDeleteValue" value="3"/>
  15.    <input type=button  onclick="deletecID()">
  16.    </form>
  17.  


getElementsByTagName will always return you an array
  • radnor
  • Beginner
  • Beginner
  • No Avatar
  • Joined: 06 Mar 2006
  • Posts: 39
  • Status: Offline

Post May 8th, 2008, 8:17 am

I tried the 1st example and figured out it creates an array for multi elements. Then take all but one away, no array as Dimat said.
  1. alert(document.frmlist.intDeleteValue.length);
Will answer this.

The 2nd example by Dimat does the EXACT thing as the 1st in my testing. Multiple input statements, an array is created. ONE input statement, no array. My alert above returns undefined.

Tested on Linux & FF
  • dimat
  • Student
  • Student
  • User avatar
  • Joined: 02 Aug 2006
  • Posts: 67
  • Loc: Kiev, Ukraine
  • Status: Offline

Post May 10th, 2008, 3:07 pm

TO: radnor

launch these:
[code]
<form name="frmlist">
<input type=radio name="intDeleteValue" value="1">

</form>
<script>
elems = document.getElementsByTagName("input");
alert(elems);
</script>
[/code]
[code]
<script>
function deletecID(){
elems = document.getElementsByTagName("input");
for(i=0; i<elems.length; ++i)
if(elems[i].name="intDeleteValue"&&elems[i].checked==true) alert(elems[i].value);
}

</script>

<form name="frmlist">
<input type=radio name="intDeleteValue" value="beer">
<input type=button onclick="deletecID()">
</form>
[/code]

what each of them tells you?
Let it be beer...

Post Information

  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© Unmelted Enterprises 1998-2008. Driven by phpBB © 2001-2008 phpBB Group.

 
 
 

Need a pre-made web design for your website?

Check out our templates here: Ozzu Templates

Perfect Money : Swiss approach to work with e-finance