www.webdeveloper.com
Results 1 to 4 of 4

Thread: Problem accessing elements of a form

  1. #1
    Join Date
    Dec 2013
    Posts
    10

    Problem accessing elements of a form

    I have a small radio button list in a form, and I am trying to access the elements in it. My alert (shown at the bottom) keeps coming back undefined. I also tried accessing the form instead of the list by using getElementById('question0') but that returned me a confusing result of '7'. Can somebody tell me what I am doing wrong?


    Code:
    <form class="quest" id="question0" style="visibility: hidden">
    <fieldset>
    <legend>Question0</legend>
    <ul class="inside" id="quest0">
    <li><input type="radio" value="A"> A</li>
    <li><input type="radio" value="B"> B</li>
    <li><input type="radio" value="C"> C</li>
    <li><input type="radio" value="D"> D</li>
    </ul>
    </fieldset>
    <p id="submit"><input type="button" onclick="cycleQuest()" value="Submit"><input type="reset" style="float: right"></p>
    </form>
    
    
    function cycleQuest() 
    {
    var textField = document.getElementById('quest0');
    alert(textField.length);
    }

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,502
    Quote Originally Posted by EdFactor View Post
    I have a small radio button list in a form, and I am trying to access the elements in it. My alert (shown at the bottom) keeps coming back undefined. I also tried accessing the form instead of the list by using getElementById('question0') but that returned me a confusing result of '7'. Can somebody tell me what I am doing wrong?


    Code:
    <form class="quest" id="question0" style="visibility: hidden">
    <fieldset>
    <legend>Question0</legend>
    <ul class="inside" id="quest0">
    <li><input type="radio" value="A"> A</li>
    <li><input type="radio" value="B"> B</li>
    <li><input type="radio" value="C"> C</li>
    <li><input type="radio" value="D"> D</li>
    </ul>
    </fieldset>
    <p id="submit"><input type="button" onclick="cycleQuest()" value="Submit"><input type="reset" style="float: right"></p>
    </form>
    
    
    function cycleQuest() 
    {
    var textField = document.getElementById('quest0');
    alert(textField.length);
    }
    Your function is not enclosed in <script></script> tags
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  3. #3
    Join Date
    Dec 2013
    Posts
    10
    my function is in a .js file, I was only providing the part of the file that was being used.

  4. #4
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,502
    you need to have a name tag for your radioset, you can not access a set of elements of a form from a list.

    Some reading for you : http://www.w3schools.com/jsref/coll_form_elements.asp

    radio object: http://www.w3schools.com/jsref/dom_obj_radio.asp

    and

    form validation : http://www.w3schools.com/js/js_form_validation.asp
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles