Haran Murthy
Haran Murthy

Reputation: 341

Retrieve radiobuttons from the child div within the main div

I have the following html.. 1 main div having 2 child divs. I need to loop through both the divs and retrieve the checked radiobutton in each. e.g.: 1st child div has maybe checked while 2nd div has wright checked, i need to retrieve both their values. How do i go about it

<DIV>
 <DIV>
  <TABLE>
   <TBODY>
      <TR>
          <TD>
            wrong<INPUT class=XX disabled value=wrong type=radio name=radiobutton>
            wright<INPUT class=XX disabled value=wright type=radio name=radiobutton>
            maybe<INPUT class=XX value=maybe CHECKED type=radio name=radiobutton ></TD>
    </TR></TBODY></TABLE>
</DIV>
<DIV>
   <TABLE><TBODY><TR>
     <TD>
      wrong<INPUT class=XX CHECKED value=wrong type=radio name=radiobutton>
       wright<INPUT class=XX value=wright type=radio name=radiobutton>
       maybe<INPUT class=XX value=maybe type=radio name=radiobutton ></TD>
       </TR></TBODY></TABLE>
     </DIV>
</DIV>

Upvotes: 1

Views: 105

Answers (2)

Tats_innit
Tats_innit

Reputation: 34107

Is this what you are looking for: Working demo http://jsfiddle.net/79syA/

In your HTML you only have wrong input radio checked, also make your html lowercase is there any reason why it is upper case :)

Rest hope this fits your need & feel free to play around with demo! :)

code

$('.XX').each(function(){

    if($(this).is(':checked')){
       alert(" Checked value is ==> " + this.value);
    }        

})​

Upvotes: 0

silentw
silentw

Reputation: 4885

You have two groups of radiobuttons from what I understand.

So, here's my solution:

HTML

<div>
  <div>
      <table>
          <tbody>
              <tr>
                  <td>wrong
                      <input class="xx" disabled="disabled" value="wrong" type="radio"
                      name="radiobutton">wright
                      <input class="xx" disabled="disabled" value="wright" type="radio"
                      name="radiobutton">maybe
                      <input class="xx" value="maybe" checked="checked" type="radio" name="radiobutton">
                  </td>
              </tr>
          </tbody>
      </table>
  </div>
  <div>
      <table>
          <tbody>
              <tr>
                  <td>wrong
                      <input class="xx" checked="checked" value="wrong" type="radio" name="radiobutton2">wright
                      <input class="xx" value="wright" type="radio" name="radiobutton2">maybe
                      <input class="xx" value="maybe" type="radio" name="radiobutton2">
                  </td>
              </tr>
          </tbody>
      </table>
  </div>
</div>​

jQuery

var radiobutton = $("input[name='radiobutton']:checked").val();
var radiobutton2 = $("input[name='radiobutton2']:checked").val();

alert('1st - '+radiobutton+' / 2nd - '+radiobutton2);​

DEMO

EDIT

jQuery

var checked = new Array();
var pos = 0;
$("input[type='radio']:checked").each(function(){
    checked[pos] = new Array(2);
    checked[pos]['name'] = $(this).attr('name');
    checked[pos]['value'] = $(this).val();
    pos++;
});

var result = "";
for(i=0; i<pos; i++){
    result += checked[i]['name'] + " - " + checked[i]['value'] + " / ";
}
alert(result);

DEMO

Upvotes: 1

Related Questions