1

Here is my HTML part:

 <input type="radio" name="checkbox_1"  id="checkbox_1" value="john" emp-id="john">
 <input type="radio" name="checkbox_1"  id="checkbox_2" value="john" emp-id="jim">
 <input type="radio" name="checkbox_1"  id="checkbox_3" value="john" emp-id="sam">
 <input type="radio" name="checkbox_1"  id="checkbox_4" value="john" emp-id="George">

I want to compare value and emp-id and if it is true, i need the checkbox to be checked so that checkbox will be displayed in the frontend as checked.

0

3 Answers 3

1

You could filter radios:

$(':radio').filter(function(){
   return $(this).attr("emp-id") === this.value
}).prop('checked', true);

Wrap it inside document ready handler if needed.

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

Comments

0

First change the HTML to use checkboxes:

HTML

<input type="checkbox" name="checkbox_1"  id="checkbox_1" value="john" emp-id="john">
<input type="checkbox" name="checkbox_1"  id="checkbox_2" value="john" emp-id="jim">
<input type="checkbox" name="checkbox_1"  id="checkbox_3" value="john" emp-id="sam">
<input type="checkbox" name="checkbox_1"  id="checkbox_4" value="john" emp-id="George">

Javascript

$(document).ready(function(){
    $("input[name='checkbox_1']").each(function(i,e){
       var el = $(this);
        this.checked = el.attr("emp-id") == el.val();
    });
});

JS Fiddle: http://jsfiddle.net/MFpUQ/3/

4 Comments

I want to load on the page initially not when clicked on the checkbox
I want the checkbox to be checked when i visit the page itself..Now, when i click on the particular button it shows checked..but i need without the clicking..\
thanks..it worked. I need to know whether we can make this to work in select option so that if it is true the select option will display it automatically..
Here is the fiddleof what i need jsfiddle.net/STk7k . I want to compare the emp-id and value. It it is true i need the answer to be shown on select option automatically.
0

html:

<input type="radio" ... checked>

javascript:

document.getElementById("checkbox_3").checked=true;

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.