SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2001
    Location
    Northern Virginia
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    radio button values

    Hello all:
    I have a problem that when reading one of my books I was not sure how to solve.
    I am dynamically creating radio buttons on my page.
    I use cold fusion to create a loop query (cfoutput query). I can have one or two radio buttons with seperate values. I need to be able to capture in JS what the value is of the selected radio button. I tried something like this:

    (eval("document.forms[0].YIELDCODE" + "[" + j + "].checked")

    but that will give me a true and a false value for the two radio buttons. I also tried this:

    (eval("document.forms[0].YIELDCODE.value")

    but that gave me an undefined value. is there a way to capture the value of the radio button I select?

    Thanks --- this is a part of a larger post from a few days ago but this part may help me solve the other post without haveing to post again.

    dan

  2. #2
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're on the right track

    You could test for the value of the checked input.
    I *thought* that document.all.checkboxname.value would
    return the value of the checked element?

    Failing that just loop through the elements and return the value of the one that is checked

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  3. #3
    ********* obeah makeda's Avatar
    Join Date
    Jun 2001
    Location
    rollin' on dubs
    Posts
    492
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Flawless is right, find the one that is checked and then get the value. So if:

    (eval("document.forms[0].YIELDCODE[1].checked")

    evaluates to True, you get the value like so:

    (eval("document.forms[0].YIELDCODE[1].value")

  4. #4
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Makeda,
    doesn't the nameoffield.value return the object value to?

    if not, maybe selectedIndex (reserved for selects) might do it?

    Essentially though, looping works.

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  5. #5
    ********* obeah makeda's Avatar
    Join Date
    Jun 2001
    Location
    rollin' on dubs
    Posts
    492
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Flawless,

    No. I thought that 'nameoffield.value' worked for radio buttons up until this week (when someone called me on it). It turns out that if you have something like this (and we're assuming that we know that someone clicked one):

    <input type="radio" name="test" value="1" />
    <input type="radio" name="test" value="2" />
    <input type="radio" name="test" value="3" />

    If you have a javascript function and try this:

    var tester = form.test.value;

    tester will evaluate as undefined. You need to tell it which index you want the value for. hence the need for a loop. Or, you could add an onclick event for the radio buttons that tracks which 'selectedIndex' is currently checked. Then when you go to get the value you wouldn't have to loop.

    PS - never tried the selectedIndex thing. i wonder if that does work?

  6. #6
    SitePoint Enthusiast
    Join Date
    Sep 2001
    Location
    Northern Virginia
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    here is the code I am using to try to get the value of the checked radion button:

    for (j = 0; j < 2; j++)
    {
    if (eval("document.forms[0].YIELDCODE" + "[" + j + "].checked"))

    { yldcode = (eval("document.forms[0].YIELDCODE[1].value"));
    alert(yldcode);
    }

    I have two radio buttons. One should have a value of 9 and the other one I think has a value of 1. I need to get the value of 9. when the alert displayed it only showed a 1 for the value no matter which radio button I checked.
    what do you'll think?

    Dan

  7. #7
    ********* obeah makeda's Avatar
    Join Date
    Jun 2001
    Location
    rollin' on dubs
    Posts
    492
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thats because you hardcoded the index. Try this:

    Code:
    for (j = 0; j < 2; j++) 
    { 
    if (eval("document.forms[0].YIELDCODE" + "[" + j + "].checked")) 
    
    { yldcode = (eval("document.forms[0].YIELDCODE[" + j + "].value")); 
    alert(yldcode); 
    }

  8. #8
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well if you're not in NN:

    Code:
    for (i=0;i<2;i++){
     if (document.getElementById('Yieldfield'+i).checked){
      alert(document.getElmentById('Yieldfield'+i.value);
      }
     }
    (that works on id's)

    Failing that... loop the two and alert the names of the tags to check you are looking in the right place.

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  9. #9
    SitePoint Enthusiast
    Join Date
    Sep 2001
    Location
    Northern Virginia
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well since you guys/gals are so smart (you solved my first problem), how about this radio button question.

    I have a set of two radio buttons one with a value of R and the other with a value of Y.
    But there can be multiple set on a page that is why they are in a loop based on how many lines(num) should be on the page.

    for (i=1;i<num+1;i++)
    { var strNum = eval("document.receipt.NumericAlts"+i+".value");

    if (eval("document.receipt.comp_condition"+i+".checked"))
    {
    for (p = 0; p < 2; p++) {
    comp_cond = (eval("document.forms[0].comp_condition"+i+"[" + p + "].value"));
    alert(comp_cond);
    }
    }

    The code shows my attempt to find the value of the radio button. I can get inside the first If so the JS function can find that one of the two radio buttons is checked. I need to find out the value next. I tried using code a previous post, but that did not seem to work.
    What do ya all think?

    Dan

  10. #10
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've got a feeling we're going from london to paris via jamaca here!

    What is it you're actually trying to achieve.

    When you're posting code.. please put [ C O D E ] [ / C O D E ] around it so we can see the nesting and don't get a worse headache than our hangovers have already given us trying to read it


    thanks

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  11. #11
    SitePoint Enthusiast
    Join Date
    Sep 2001
    Location
    Northern Virginia
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry my fault.

    -----code------

    for (i=1;i<num+1;i++)
    {
    var strNum = eval("document.receipt.NumericAlts"+i+".value");

    if (eval("document.receipt.comp_condition"+i+".checked"))
    {
    for (p = 0; p < 2; p++) {
    comp_cond = (eval("document.forms[0].comp_condition"+i+"[" + p + "].value"));
    alert(comp_cond);
    }
    }

    -------code--------

  12. #12
    SitePoint Enthusiast
    Join Date
    Sep 2001
    Location
    Northern Virginia
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry did not answer your question.

    I have different items on my page - some radio buttons, some text fields, and some dropdown boxes. depending on what you select for one, something has to be selected for the others.
    All these items are within a loop. these items could be on the page multiple times that is why I have in the above code comp_condition+i...where i relates to a looping variable.

    right now I have two radio buttons that if the user selects one with a 9 value, the user must select a string in the dropdown box that has a 9** as the last value in the displaying string. Now the user must also select another radio button that has a value of R. That is why I posted the above code. there are two radio buttons one yellow(y)default and one red(r).
    Kinda confusing, but hope this makes more sense. I feel I am close with the code in the previous posting.

    Thanks for the code tip and for reading this crazy problem.

    Dan

  13. #13
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Could you use a generic function with a set of arrays to hide / display the relevant boxes depending on selection, and then add them to an array for ID checking on submit?

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---


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
  •