0

Look to my code below :

HTML :

<input type="checkbox" name="xyz[1][]" id="sel_44" style="margin:2px;" value="12345" onclick="myClick(this)">

Javascript :

<script>
$('#sel_44').attr("checked", true);
</script>

I already try every method (method that suggest by acceptance answer) in this URL : Check/Uncheck checkbox with javascript?

My Problem and Question :

I can sure $('#sel_loc_cb_44') is not null and not undefined. But i can make checkmark via javascript or jquery. How to fix my code and what's the source of problem?

Please kindly add jsfiddle to your solution post. thank you

Community
  • 1
  • 1
Mr. Mike
  • 453
  • 5
  • 23

5 Answers5

2

try using

$('#sel_44').prop("checked", true);

you must change the propery of the DOM object instead of attribute

1

Since jquery 1.6+ prop method provides a way to explicitly retrieve property values, while attr retrieves attributes. Here checked is a property. So use prop for checking a checkbox.

$('#sel_44').prop("checked", true);
brk
  • 48,835
  • 10
  • 56
  • 78
1

try this with jquery:

$("#sel_44").prop('checked',true);
Abanoub Makram
  • 463
  • 2
  • 13
0

Try this

$(document).ready(function() {
$('#sel_44').attr("checked", true);
});

I have tried in the jsfiddler, looks like you need to put your code inside the document ready function.

https://jsfiddle.net/o2gxgz9r/5935/

Naveen K N
  • 180
  • 1
  • 11
  • I don't want to do that when clicked. I want to checked when the first time it showed but using Javascript not write the DOM checked="checked" – Mr. Mike Apr 20 '17 at 09:49
0

$(function(){
  $('#sel_44').prop('checked',true);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="xyz[1][]" id="sel_44" style="margin:2px;" value="12345">
Brissy
  • 349
  • 1
  • 2
  • 10
  • I don't want to do that when clicked. I want to checked when the first time it showed but using Javascript not write the DOM checked="checked" – Mr. Mike Apr 20 '17 at 09:49