Checkbox selected in function

0

I have a function and I'm trying to set the checkbox to true, in function, this way:

 $("#cbpre").checked = true;

But it is not scoring, I've debugged, and it goes into this part, it just does not checkbox.

    
asked by anonymous 02.07.2018 / 15:19

2 answers

3

.prop ()

 
  $("#cbpre").prop('checked', true);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputid="cbpre"" type="checkbox">

OU

.attr ()

 $("#cbpre").attr('checked','checked')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputid="cbpre"" type="checkbox">

What is the difference between .prop () and .attr ()?

    
02.07.2018 / 15:25
2

You can use the attr method or the prop method of JQuery for this:

$('#ck1').attr('checked', 'checked');
$('#ck2').prop('checked', true);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><p>Checkbox1:<inputid='ck1'type="checkbox" />
</p>
<p>
   Checkbox 2: <input id='ck2' type="checkbox" />
</p>
    
02.07.2018 / 15:33