0

When I click on the checkbox the banner (alert()) pop up.

But when I checked the checkbox with jQuery, the banner did not pop up.

How to make input tag trigger the onclick function when the checkbox is checked in jQuery? Or is there any alternative way?

Thanks in advance

<input type="checkbox" onclick="alert()" id="alert" checked>

$("#alert").prop("checked", true);

function alert() {
   ...something
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Jeff
  • 59
  • 6

2 Answers2

0

$('#alert').on('change' function(){alert(whatever...)})

Savvii
  • 480
  • 4
  • 9
0

Use on('click',function()) to do everything you want once the checkbox is clicked.

$('button').on('click', function(){   $('input').click()    });

// Do this when the checkbox input is clicked by jQuery
$('input').on('click', function(){
 console.log('THIS COULD BE A POPUP')
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<button>check box with jQuery</button>
<input type="checkbox">
Gass
  • 7,536
  • 3
  • 37
  • 41