1

I have a simple form with radio buttons

<form>
<input type="radio" name="radio_1" value="1" />Radio 1
<input type="radio" name="radio_1" value="2" />Radio 2
<input type="radio" name="radio_1" value="3" />Radio 3
</form>

And i have additional div with same radios (without form)

<div class="attributes">
<input type="radio" name="radio_1" value="1" />Radio 1
<input type="radio" name="radio_1" value="2" />Radio 2
<input type="radio" name="radio_1" value="3" />Radio 3
</div>

I want to make radios with same name and value in FORM to be checked when i check radios in DIV

How can i make it?

1
  • Using jQuery, select the div inputs: $("div input[type='radio']") then add a click method to them and when you click those radio buttons, it checks all others, compares the name and value to other radios, and checks them if they match. Commented Aug 22, 2016 at 13:17

5 Answers 5

3

Attach a change() event handler then get the corresponding element in form using attribute equals selector and set it's checked property using prop() method.

$('.attributes :radio').change(function() {
  $('form [name="radio_1"][value="' + this.value + '"]').prop('checked', this.checked);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  <input type="radio" name="radio_1" value="1" />Radio 1
  <input type="radio" name="radio_1" value="2" />Radio 2
  <input type="radio" name="radio_1" value="3" />Radio 3
</form>

<div class="attributes">
  <input type="radio" name="radio_1" value="1" />Radio 1
  <input type="radio" name="radio_1" value="2" />Radio 2
  <input type="radio" name="radio_1" value="3" />Radio 3
</div>

Sign up to request clarification or add additional context in comments.

Comments

1

Use this code :

$(".attributes :radio").on("change",function(){

$("form input[value = " + $(this).val() + "]").prop("checked",$(this).prop("checked"));

})

Final code :

<html>
    <head>
        <meta charset="utf-8">
    </head>
    <body>
        <form>
            <input type="radio" name="radio_1" value="1" />Radio 1
            <input type="radio" name="radio_1" value="2" />Radio 2
            <input type="radio" name="radio_1" value="3" />Radio 3
        </form>
        <div class="attributes">
            <input type="radio" name="radio_1" value="1" />Radio 1
            <input type="radio" name="radio_1" value="2" />Radio 2
            <input type="radio" name="radio_1" value="3" />Radio 3
        </div>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
        <script>
            $(document).ready(function(){
               $(".attributes :radio").on("change",function(){
                   $("form input[value = " + $(this).val() + "]").prop("checked",$(this).prop("checked"));
               })
            })
        </script>
    </body>
</html>

Comments

1

This should do it! It will work with multiple groups of checkboxes as demonstrated in the demo.

    $('div :radio').change(function() {
        $('form :radio[name="' + this.name + '"][value="' + this.value + '"]').prop('checked', true);
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
A: <input type="radio" name="radio_1" value="1" />Radio 1
<input type="radio" name="radio_1" value="2" />Radio 2
<input type="radio" name="radio_1" value="3" />Radio 3
  
  <br>
B: <input type="radio" name="radio_2" value="1" />Radio 1
<input type="radio" name="radio_2" value="2" />Radio 2
<input type="radio" name="radio_2" value="3" />Radio 3
</form>

<div class="attributes">
A: <input type="radio" name="radio_1" value="1" />Radio 1
<input type="radio" name="radio_1" value="2" />Radio 2
<input type="radio" name="radio_1" value="3" />Radio 3
    <br>
B: <input type="radio" name="radio_2" value="1" />Radio 1
<input type="radio" name="radio_2" value="2" />Radio 2
<input type="radio" name="radio_2" value="3" />Radio 3
</div>

Comments

0

There are a few ways to do it, but simple way would be to make a selector based on name and value.

$('input[type="radio"]').on("change", function () {
     $('input[type="radio"][name="'+this.name+'"][value="'+this.value+'"]').prop("checked", true);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form>
<input type="radio" name="radio_1" value="1" />Radio 1
<input type="radio" name="radio_1" value="2" />Radio 2
<input type="radio" name="radio_1" value="3" />Radio 3
</form>


<div class="attributes">
<input type="radio" name="radio_1" value="1" />Radio 1
<input type="radio" name="radio_1" value="2" />Radio 2
<input type="radio" name="radio_1" value="3" />Radio 3
</div>

Comments

0

You can use <lable> tag and add for attribute which refers identifier of radio button. as mentioned below

Also I have added javascript for select same radia button vise-e-versa.

$('form input[type="radio"], attribute input[type="radio"]').click(function(){
  //alert($(this).attr('name')+"::"+$(this).attr('value'));
 $('input[name="'+$(this).attr('name')+'"][value="'+$(this).attr('value')+'"]').not(this).trigger('click');
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<form>
<input type="radio" name="radio_1" id="radio_11" value="1" /><label for="radio_11" >Radio 1</label>
<input type="radio" name="radio_1" id="radio_12" value="2" /><label for="radio_12" >Radio 1</label>
<input type="radio" name="radio_1" id="radio_13" value="3" /><label for="radio_13" >Radio 1</label>
</form>

<div class="attributes">
<input type="radio" name="radio_1" id="radio_21" value="1" /><label for="radio_21" >Radio 1</label>
<input type="radio" name="radio_1" id="radio_22" value="2" /><label for="radio_22" >Radio 1</label>
<input type="radio" name="radio_1" id="radio_23" value="3" /><label for="radio_23" >Radio 1</label>
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.