noob-1
noob-1

Reputation: 23

How to select and check these types of checkboxes based on values?

<span class="answerBox">
<a name="checkboxtag" class="largecheckbox" onclick="selectQuestionAnswer(this);"></a>
<input name="answers" value="8ad6a4124641ec5001464d856b23436c" class="checkbox displaynone" type="radio">
</span>

I've tried using .prop()

jQuery('input:checkbox[value="8ad6a4124641ec5001464d856b23436c"]').prop("checked", true);

Tried to just trigger a click:

jQuery('input:checkbox[value="8ad6a4124641ec5001464d856b23436c"]').trigger('click');

Tried editing its sibling's class (because on click, it changes the sibling's class and nothing else):

jQuery('input:checkbox[value="8ad6a4124641ec5001464d856b23436c"]').siblings('.largecheckbox').trigger('click');

Then I wasn't so sure if I was even selecting it correctly. I tried to toggle highlight on the checkbox but it doesn't do anything..

jQuery('input:checkbox[value="8ad6a4124641ec5001464d856b23436c"]').siblings('.largecheckbox').toggle('highlight');
jQuery('input:checkbox[value="8ad6a4124641ec5001464d856b23436c"]').toggle('highlight');

Is it just not possible to select this checkbox? I've been trying all day yesterday and today.

Thanks for reading and any help!

Edit: you guys are gods. IT WORKS. I used jQuery('input:radio[value="8ad6a4124641ec5001464d856b23436c"]').siblings().toggle('highlight');

to just make sure it worked AND IT DOES. THANK YOU. Then I used trigger('click'); and it did. All I had to do was select it using the radio selector. Thank you everyone for your efforts.

Upvotes: 2

Views: 56

Answers (5)

遁地龙卷风
遁地龙卷风

Reputation: 40

You don't have a matching element,to be changed$('input:radio[value="8ad6a4124641ec5001464d856b23436c"]')

Upvotes: 0

charlietfl
charlietfl

Reputation: 171690

You are trying to change an <input type="radio"> with a checkbox selector

Fix one or the other to what it is supposed to be

Your code works fine when you change to

<input name="answers" value="8ad6a4124641ec5001464d856b23436c" type="checkbox">

If you want to leave as radio use :radio selector

DEMO

Upvotes: 1

Nadeem Manzoor
Nadeem Manzoor

Reputation: 750

I am using this function to check any checkbox you need to pass jquery object of the checkbox

function toggleCheck(elm, checked) {
    if (elm.is(":checked") != checked) {
        elm.click();
    }
}

toggleCheck($('[name="answers]'), true) // will check if not already checked
toggleCheck($('[name="answers]'), false) // will un check if already checked

Upvotes: 0

davidatthepark
davidatthepark

Reputation: 1365

Your first solution was very close. Just change the colon to a period.

jQuery('input.checkbox[value="8ad6a4124641ec5001464d856b23436c"]').prop("checked", true);

Upvotes: 0

Besi
Besi

Reputation: 330

Try this, after you change the radio to checkbox.

$(function(){
    $(".largecheckbox").click(function(){
        $(this).next("input:checkbox").attr("checked",true);
    });
});

Upvotes: 0

Related Questions