RedGiant
RedGiant

Reputation: 4748

Find input box of a specific type as well as its value

Please take a look at this fiddle.

I can use the following to find an input box with a specific value:

$('input[value="'+findtext+'"]');

but is it possible to find an input box of a specific type as well? This one won't work in the example:

$('input[type="checkbox" value="'+findtext+'"]');

HTML:

<button class="check">AAA</button>

<input type="radio" value="AAA">This
<input type="checkbox" value="BBB">This

Script:

$('.check').click(function(){
   var findtext = $(this).text();console.log(findtext);
   $('input[type="checkbox" value="'+findtext+'"]').attr('checked', true);

});

Upvotes: 0

Views: 41

Answers (1)

Dalorzo
Dalorzo

Reputation: 20014

Sounds like this will do the trick: $('input[value="BBB"][type=checkbox]')

Online Demo

$('.check').click(function(){
   var findtext = $(this).text();console.log(findtext);
   $('input[value="' + findtext  + '"][type=checkbox]').attr('checked', true); 
});

This is subject of multiple attribute selectors with JQuery in case you want to read more about it.

Upvotes: 2

Related Questions