SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist
    Join Date
    Dec 2005
    Posts
    527
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Selected Radio button

    How can I found the selected Item ?
    My Items genrate by PHP

    this is my code :

    Code:
    len = document.getElementByName('poll_radio').lenght;
    for (i = 0; i <len; i++) {
    	if (document.getElementByName('poll_radio')[i].checked) {
    		chosen = document.getElementByName('poll_radio')[i].value
    	}
    }
    But can not work in IE7

  2. #2
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    document.getElementsByName
    Code:
     
    <script type="text/javascript">
    function showChecked(){
    var  r,  i = 0; 
    var el = document.getElementsByName('poll_radio')
    while(r = el[i++]) { if(r.checked) { alert(r.value); } }
    }
    </script>
    
    <input type="radio"  name="poll_radio" value ="1" onclick="showChecked()">
    <input type="radio"  name="poll_radio" value ="2" onclick="showChecked()">
    <input type="radio"  name="poll_radio" value ="3" onclick="showChecked()">
    <input type="radio"  name="poll_radio" value ="4" onclick="showChecked()">
    Code:
      
    <script type="text/javascript">
    function showChecked(){
    var chosen;
    var el = document.getElementsByName("poll_radio");
    var i;
    for(i = 0; i < el.length; i++){
    if(el[i].checked) { chosen = el[i].value; alert(chosen); }
    }
    }
    </script>
    
    <input type="radio" name="poll_radio" value ="1" onclick="showChecked()">
    <input type="radio"  name="poll_radio" value ="2" onclick="showChecked()">
    <input type="radio"  name="poll_radio" value ="3" onclick="showChecked()">
    <input type="radio"  name="poll_radio" value ="4" onclick="showChecked()">
    Code:
     
    <script type="text/javascript">
    function showChecked(){
    var chosen;
    len = document.getElementsByName('poll_radio').length;
    for (i = 0; i <len; i++) {
    	if (document.getElementsByName('poll_radio')[i].checked) {
    		chosen = document.getElementsByName('poll_radio')[i].value;
     alert(chosen);
    	}
    }
    }
    
    </script>
    
    <input type="radio"  name="poll_radio" value ="1" onclick="showChecked()">
    <input type="radio"  name="poll_radio" value ="2" onclick="showChecked()">
    <input type="radio"  name="poll_radio" value ="3" onclick="showChecked()">
    <input type="radio"  name="poll_radio" value ="4" onclick="showChecked()">
    Last edited by muazzez; Apr 19, 2007 at 04:19.

  3. #3
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    http://www.somacon.com/p143.php

    Does the radio group have only one element in it?

    Edit:

    Good spot!
    Ian Anderson
    www.siteguru.co.uk

  4. #4
    SitePoint Evangelist
    Join Date
    Dec 2005
    Posts
    527
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no !

    now I have problem with : .length !

  5. #5
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's spelt wrong in the code above.
    You have lenght instead of length

  6. #6
    SitePoint Evangelist
    Join Date
    Dec 2005
    Posts
    527
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My English is so pooooooooor I need an English class not Javascript class

    thank you very much


Bookmarks

Posting Permissions

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