Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onClick function returns undefined

For some reason, the following code does not work properly:

<input onclick="$(function(){ alert($(this).attr('checked')); })" name="step1[agree]" id="step1_agree" value="1" type="checkbox">

It alerts with undefined. I have also tried doing something like this:

onclick="$(function(){ var chk=$(this); alert(chk.attr('id')); })"

... but ends up with the same result. What am I doing wrong and how can I fix this?

like image 286
kusanagi Avatar asked Dec 09 '25 10:12

kusanagi


1 Answers

<input onclick="alert($(this).attr('checked'));" name="step1[agree]" id="step1_agree" value="1" type="checkbox">

But a better option would be

<input name="step1[agree]" id="step1_agree" value="1" type="checkbox">

$('#step1_agree').click(function() {
  alert($(this).attr('checked));
});
like image 84
devmatt Avatar answered Dec 12 '25 00:12

devmatt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!