techiva.blogspot.com
techiva.blogspot.com

Reputation: 1180

Dynamically pass clicked anchor tag name using Javascript

Hi Everyone I am getting dynamic labors_id now i want to pass this variable labors_id in input[name=labors_id] field but this is not working

HTML

 <a id="location[]" class="btn btn-success date-days check-all"  href="javascript:void(0);">Select All</a>

Javascript

$(document).ready(function() {
 $('.check-all').click(function(){
  var labors_id=this.id;
  alert(labors_id);
  // here I am trying to pass labors_id below but it's not working
  $("input[name=labors_id]").attr('checked', true);

 });
});

please suggest something how i can pass labors_id variable in input. thank you

Upvotes: 0

Views: 215

Answers (4)

KAD
KAD

Reputation: 11112

You have to concatenate the variable within the CSS selector in order to get the checkboxes with name labor_id, also you need to use the jquery prop and not attr.

$("input[name='"+ labors_id +"']").prop('checked', true)

Natively speaking, attribute describes the initial/default attribute value of the element while property has the current value/state. Besides, the attribute accepts only string values while property accepts objects, boolean and other types.

Upvotes: 1

user996581
user996581

Reputation: 155

Works for all j Query version:

$("input[name='"+ labors_id +"']").each ( function ( idx, data ) 
{
 data.checked = true;
});

Upvotes: 0

Murali Murugesan
Murali Murugesan

Reputation: 22619

To get the checked property(true/false) of the clicked check box

Try

$(document).ready(function() {
  $('.check-all').click(function() {
    var labors_id = this.id;
    $("input[name='"+ labors_id +"']").prop('checked');
  });
});

Upvotes: 1

Ali Hesari
Ali Hesari

Reputation: 1939

use .val() for pass id value to input: http://api.jquery.com/val/

  $("input[name=labors_id]").val(labors_id).attr('checked', true);

Upvotes: 0

Related Questions