Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8

Thread: Bug in JS ???

  1. #1
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    911
    Thanks
    302
    Thanked 2 Times in 2 Posts

    Question Bug in JS ???

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

    Code:
    <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.


    Code:
    <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

  • #2
    Regular Coder
    Join Date
    Nov 2007
    Location
    127.0.0.1
    Posts
    348
    Thanks
    26
    Thanked 40 Times in 39 Posts
    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() method.
    Last edited by tagnu; 05-08-2008 at 01:02 PM.
    Blog Charity:Water
    WhatisWrongWith.me/tagnu - Send me anonymous feedback.

  • #3
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    911
    Thanks
    302
    Thanked 2 Times in 2 Posts
    True but this does not

    Code:
    <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?

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,922
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    I don't think it is a bug - radio buttons are supposed to form a group, i.e. more than one selection.

    Code:
    <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>

  • Users who have thanked Philip M for this post:

    phantom007 (05-08-2008)

  • #5
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    911
    Thanks
    302
    Thanked 2 Times in 2 Posts
    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?

  • #6
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Use getElementsByName() method.
    Code:
    <!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>
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #7
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    911
    Thanks
    302
    Thanked 2 Times in 2 Posts
    Thanx Kor, your code works fine.

    So tell me was it a bug that I discovered?

  • #8
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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.
    Last edited by Kor; 05-08-2008 at 02:50 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •