likephp
likephp

Reputation: 13

Auto change radio selection on another form

I have two forms in one webpage like

<tr>
  <td>
    <h4>Team A[Form1]</h4>
    <form name="form1" enctype="multipart/form-data">
     <input type="radio" value="batting" name="teamA" /> Batting<br />
      <input type="radio" value="bowling" name="teamA" /> Bowling<br />
    </form>
  </td>
  <td>
    <h4>Team B[Form2]</h4>
    <form name="form2" enctype="multipart/form-data">
     <input type="radio" value="batting" name="teamB" /> Batting<br />
      <input type="radio" value="bowling" name="teamB" /> Bowling<br />
    </form>
  </td>
 </tr>

and how to auto select second form radio button on form1 selection. Change form2 button as Bowling selected if I select Batting in form1.

I am trying to do like

<script type="text/javascript">
     if(document.form1.teamA.checked = 'batting') {
         document.form2.teamB.checked = 'bowling';
     }
    </script>

Upvotes: 0

Views: 931

Answers (4)

dfsq
dfsq

Reputation: 193291

You can use data attributes to define what field should be "connected" to which button:

<form name="form1" id="form1" enctype="multipart/form-data">
    <input type="radio" value="batting" data-toggle="bowling" name="teamA" />Batting
    <br />
    <input type="radio" value="bowling" data-toggle="batting" name="teamA" />Bowling
    <br />
</form>

JS

$('#form1 :radio').change(function () {
    $('#form2 :radio').filter('.' + $(this).data('toggle')).prop('checked', true);
});

http://jsfiddle.net/hVkmM/

So if one day the value changes or new options are added javascript code will continue to work.

Or like this:

$('#form1 :radio').change(function () {
    $('#form2 :radio[value="' + $(this).data('toggle') + '"]').prop('checked', true);
});

http://jsfiddle.net/hVkmM/1/

You can even make two way binding so checking radio buttons in the second form changes state in the first form:

http://jsfiddle.net/hVkmM/2/

Upvotes: 1

Muhammad Ummar
Muhammad Ummar

Reputation: 3631

Use following code

    <script>
    $(document).ready(function(){

$("input[name='teamA']").change(function(){

    if($("input[name='teamA']:checked").val() == "batting")
    {
       $("input:radio[name='teamB'][value='bowling']").attr('checked', 'checked');
    }
    if($("input[name='teamA']:checked").val() == "bowling")
    {
       $("input:radio[name='teamB'][value='batting']").attr('checked', 'checked');
    }

});


});
    </script>

Have a look on this Fiddle http://jsfiddle.net/z8Lnd/5/

Upvotes: 0

DVM
DVM

Reputation: 1238

This should work better than the previous version:

$(document).ready(function(){
 $("#form1 input:radio[id='bowling']").prop("click",true);
    $("#form1 input:radio").on("change",function(){
   $("#form2 input:radio[id='bowling']").prop("checked", $("#batting").prop("checked"));
    });
});

http://jsfiddle.net/rMYrj/2/

Also made some changes to your HTML so you can use id's in selectors

Upvotes: 0

karaxuna
karaxuna

Reputation: 26940

$('form[name="form1"] input[type=radio][name=teamA]').change(function(){
     var radiosFrom2ndForm = $('form[name="form2"] input[type=radio][name=teamB]');
     for(var i = 0; i < radiosFrom2ndForm.length; i++)
          if(radiosFrom2ndForm[i].value !== this.value)
               radiosFrom2ndForm[i].checked = true;
});

Fiddle

Alternative:

$('form[name="form1"] input[type=radio][name=teamA]').change(function(){
     $('form[name="form2"] input[type=radio][name=teamB][value!=' + this.value + ']').prop('checked', true);
});

Fiddle

Upvotes: 0

Related Questions