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?
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?
Share Improve this question edited Jun 3, 2011 at 11:28 Shaz 15.9k4 gold badges43 silver badges59 bronze badges asked Jun 3, 2011 at 11:18 kusanagikusanagi 14.6k22 gold badges89 silver badges112 bronze badges 06 Answers
Reset to default 5<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));
});
This should work.
onclick="alert(this.checked);"
Please learn about binding in jQuery.
$('#step1_agree').bind('click', function() { alert(this.checked); });
Not sure why you are wrapping it in a function like that. It should just be
onclick="alert($(this).attr('id'));"
Seems you have to use the following:
<script type="text/javascript">
$(function(){
$('#step1_agree').click(function(){
alert('')
});
})
</script>
Please refer to unobtrusive javascript
You can use something like
$(function(){
$("#step1_agree").click(function(){
alert (this.checked);
});
});
$(function(){
});
gets fired once the DOM is ready.
You must assign all event handlers inside document ready. You can use an id selector
to attach a click event handler
to the desired element. To get the checked attribute you can use the native javascript code this.checked
.