Mir Gulam Sarwar
Mir Gulam Sarwar

Reputation: 2648

Get element in a class by element's name attribute

<input type="checkbox" class="check" value="true" name="1">
<input type="checkbox" class="check" value="true" name="2">
<input type="checkbox" class="check" value="true" name="3">

Now what I want is that get the checkbox in "check" class which has name:1, or 2 or 3

I tried below but not working

var me=1;    // need to save it in a variable
$('.check[name=""+me]').prop("checked",true);    // pay attention in  +me

I it should work please let me know.Then i will look why it is not working.Thanks for having time on my issue.

Upvotes: 1

Views: 42

Answers (3)

Silviu Burcea
Silviu Burcea

Reputation: 5348

$(".check")
  .filter(function(){ 
    return $(this).attr("name") == "1"; 
  })
  .attr("checked", true);

Upvotes: 0

Milind Anantwar
Milind Anantwar

Reputation: 82231

Try this:

 var me = 1;
 $('.check[name='+me+']').prop("checked",true);

or

 $('.check[name="1"]').prop("checked",true);

Working Demo

Upvotes: 1

adeneo
adeneo

Reputation: 318172

You're almost there

var me = 1;
$('.check[name="'+me+'"]').prop("checked",true);

Upvotes: 2

Related Questions