SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Javascript is saying that Radio button selection not made even though it is made!

    Hello,

    Why is the input type = radio on this page is not getting the value assigned to it after the user clicks on the related selection?

    Here is the page under development:
    dreamdates.com - TOTALLY FREE online dating service - date singles ready for romance and love

    You can see this by selecting a choice for "I am a: " Radio button selection. I have set up Javascript code, alert, which will tell you that that selection is "Undefined" even though you have selected male or female.

    Regards,

    Anoox search engine volunteer

    www.anoox.com

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,784
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    You are testing data.mygender.value which is undefined as data.mygender is an array and not a single entry.

    You should be testing data.mygender[data.myGender.selectedIndex].value in order to read the value of the entry in the array that is currently selected.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    But mygender is an input of Radio type and NOT checkbox type, so it is not in an array.

    I mean to read its value via Php for example one would do:

    $mygender = $_POST['mygender'];

    I mean:
    1- I have never seen this in case of Radio value retrieval
    data.mygender[data.myGender.selectedIndex].value

    2- what would selectedIndex be?

    Quote Originally Posted by felgall View Post
    You are testing data.mygender.value which is undefined as data.mygender is an array and not a single entry.

    You should be testing data.mygender[data.myGender.selectedIndex].value in order to read the value of the entry in the array that is currently selected.

    Anoox search engine volunteer

    www.anoox.com

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,784
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by WorldNews View Post
    Hi,

    But mygender is an input of Radio type and NOT checkbox type, so it is not in an array.
    Your statement is correct for server side languages since only the selected entry gets passed to the server from radio buttons.

    JavaScript has to deal with all of what is in the web page though and so for JavaScript radio buttons are the ONLY form field that is ALWAYS an array because there is always more than one of them with the same name. Chechkboxes are not usually arrays since there is usually only one checkbox for each name.

    JavaScript determines which entry in a radio button array by using the selectedIndex attribute as the number representing the entry in the radio button array that is selected.

    In JavaScript radio buttons are an array because JavaScript sees all the buttons whether selected or not. To server side languages radio buttons are not an array because only the selected button is passed. You can't base how JavaScript works on server side languages because JavaScript sees the entire web page content whereas server side languages only see the values submitted back to the server.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I see.
    Ok, thanks for clarifying that.
    I tell you as a developer one learns a few new things per day

    Quote Originally Posted by felgall View Post
    Your statement is correct for server side languages since only the selected entry gets passed to the server from radio buttons.

    JavaScript has to deal with all of what is in the web page though and so for JavaScript radio buttons are the ONLY form field that is ALWAYS an array because there is always more than one of them with the same name. Chechkboxes are not usually arrays since there is usually only one checkbox for each name.

    JavaScript determines which entry in a radio button array by using the selectedIndex attribute as the number representing the entry in the radio button array that is selected.

    In JavaScript radio buttons are an array because JavaScript sees all the buttons whether selected or not. To server side languages radio buttons are not an array because only the selected button is passed. You can't base how JavaScript works on server side languages because JavaScript sees the entire web page content whereas server side languages only see the values submitted back to the server.

    Anoox search engine volunteer

    www.anoox.com


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
  •