Select to view content in your preferred language

Getting value of selected radio button

8065
8
03-22-2012 08:47 AM
SamirGambhir
Frequent Contributor
Hello,
My application has three title panes with a minimum of three radio buttons each within each. Once the user selects one radio button, and clicks on the 'Go' button, a function will get fired to display a dynamic layer on the map. I would like to retrieve the name or id of the radio button selected so I can use it as a parameter for my function. I am struggling to find a way to get the value (name, id etc.) of the selected radio button. Can someone suggest a way to do that?
Thanks
Samir
0 Kudos
8 Replies
KenDoman
Frequent Contributor
Here's a little example:
var formElements = dojo.query("input:checked");
console.log("First radio input id: ", formElements[0].id);
console.log("Second radio input name: ", formElements[1].name);
console.log("Third radio input value: ", formElements[2].value);


For more information: http://livedocs.dojotoolkit.org/dojo/query
0 Kudos
SamirGambhir
Frequent Contributor
Thanks Raymond,
I tried this in this form but it did not work:

var chkBoxes=dojo.query('input:checked','indFormM');
          dojo.forEach(chkBoxes, function(check, i){
            alert("ID = " + chkBoxes.id);
           
          });

Is there something wrong in this statement?
Thanks, appreciate your help
Samir
0 Kudos
KenDoman
Frequent Contributor
Thanks Raymond,
I tried this in this form but it did not work:

var chkBoxes=dojo.query('input:checked','indFormM');
          dojo.forEach(chkBoxes, function(check, i){
            alert("ID = " + chkBoxes.id);
           
          });

Is there something wrong in this statement?
Thanks, appreciate your help
Samir


dojo.forEach(chkBoxes, function(chkBx) {
  alert("ID = " + chkBx.id);
});
0 Kudos
SamirGambhir
Frequent Contributor
Still no luck, sorry.
Thanks for looking into it. Let me know if you come across another solution to this issue.
Samir
0 Kudos
KenDoman
Frequent Contributor
Thanks Raymond, 
I tried this in this form but it did not work: 

var chkBoxes=dojo.query('input:checked','indFormM'); 
dojo.forEach(chkBoxes, function(check, i){ 
alert("ID = " + chkBoxes.id); 
  
}); 

Is there something wrong in this statement? 
Thanks, appreciate your help 
Samir


I overlooked something earlier. Dojo.query uses CSS selectors, so to get all the checked inputs from form with id='indFormM', search for "#indFormM input:checked".

var chkBoxes=dojo.query('#indFormM input:checked'); // <- need to use CSS decendent selector
dojo.forEach(chkBoxes, function(chkBx){
  alert("ID = " + chkBx.id);
});
0 Kudos
SamirGambhir
Frequent Contributor
Hi Raymond,
Thanks for your help. Unfortunately, your code did not work for me. I tried this instead which is a longer code than yours but it works:
function getRadioValue() {
        var radioObj = dojo.byId('indFormM');
        var radioLength = radioObj.length;
        for (var i=0; i<radioLength; i++){
          if(radioObj.checked){
          alert("You checked "+radioObj.id);
         
          }
        }
      }

I have a related question. I would now like to get the id of the TitlePane in which the selected radio button resides. I am not sure how to get a handle on the id or name of the TitlePane. Any help will be appreciated.
0 Kudos
SamirGambhir
Frequent Contributor
Hello,
I am attempting to retrieve the id of the parent when a child radio button is clicked. I am using the following code but I get "null" as the result. What am I doing wrong here?

function getRadioValue() {
        var radioObj = dojo.byId('indFormM');
        var radioLength = radioObj.length;
        for (var i=0; i<radioLength; i++){
          if(radioObj.checked){
            var selRadio = radioObj.id;
            var testObj = radioObj.parentNode;
            alert("You checked "+selRadio);
            alert("Parent id is "+testObj.getAttribute('id'));
          }
        }
      }

Thanks
Samir Gambhir
0 Kudos
alexenderfleming
New Contributor
I was looking to find posts on this particular topic and your post came up first on Google search and finely i have found your post that is even slightly related to what I need Nice post. i have very impressed to see this post. i have benefited from the information you give. nice work keep it up.
0 Kudos