newbie: reading radio selection

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • r_ahimsa_m@poczta.onet.pl

    newbie: reading radio selection

    Hello,
    I am learning JavaScript. I created radiobutton group:

    <input type="radio" name="building_ type" value="N">new</input>
    <input type="radio" name="building_ type" value="O">old</input>
    <input type="radio" name="building_ type" value="" checked>n/a</input>

    and I want to test selection in JavaScript:

    if (document.annou ncement.buildin g_type.value != "") ...

    My problem is that - whatever is selected -
    document.announ cement.building _type.value is undefined.
    I expected "N", "O", "".
    Please help.
    /RAM/
  • SAM

    #2
    Re: newbie: reading radio selection

    r_ahimsa_m@pocz ta.onet.pl a écrit :
    Hello,
    I am learning JavaScript. I created radiobutton group:
    >
    <input type="radio" name="building_ type" value="N">new</input>
    <input type="radio" name="building_ type" value="O">old</input>
    <input type="radio" name="building_ type" value="" checked>n/a</input>
    >
    and I want to test selection in JavaScript:
    >
    if (document.annou ncement.buildin g_type.value != "") ...
    >
    My problem is that - whatever is selected -
    document.announ cement.building _type.value is undefined.
    to get value of any kind of form's element

    function getElementValue (elemt) {
    if(elemt.tagNam e == 'input' && elemt.length)
    { // elemt is radios or checkboxes collection
    for(var i=0, L = elemt.length; i<L; i++) {
    if(elemt[i].checked) {
    elemt = elemt[i];
    break;
    }
    }
    }
    return elemt.value;
    }


    if (getElementValu e(document.anno uncement.buildi ng_type) != "")


    --
    sm

    Comment

    • Doug Gunnoe

      #3
      Re: newbie: reading radio selection

      On Jun 21, 4:46 pm, r_ahims...@pocz ta.onet.pl wrote:
      Hello,
      I am learning JavaScript. I created radiobutton group:
      >
      <input type="radio" name="building_ type" value="N">new</input>
      <input type="radio" name="building_ type" value="O">old</input>
      <input type="radio" name="building_ type" value="" checked>n/a</input>
      >
      and I want to test selection in JavaScript:
      >
      if (document.annou ncement.buildin g_type.value != "") ...
      >
      My problem is that - whatever is selected -
      document.announ cement.building _type.value is undefined.
      I expected "N", "O", "".
      Please help.
      /RAM/
      Look at the DOM method getElementsByNa me



      You want something like,

      var obj = document.getEle mentsByName('bu ilding_type');

      then you should see that obj[0].value == "N" and obj[1].value == "O"
      and obj[2].value == "".

      Good luck.

      Comment

      Working...