I want to change the following JS to Jquery. But I don't know how to pass parameter to click event in Jquery. Can anyone help me, thanks!
<script type="text/javascript">
function display(id){
alert("The ID is "+id);
}
</script>
<input id="btn" type="button" value="click" onclick="display(this.id)" />
It's jQuery equivalent would be something like this: $('button'). on('click', myFunction); Passing an argument to that function would then be something like this: $('button'). on('click', myFunction('test')); .
Better Approach:
<script type="text/javascript">
$('#btn').click(function() {
var id = $(this).attr('id');
alert(id);
});
</script>
<input id="btn" type="button" value="click" />
But, if you REALLY need to do the click handler inline, this will work:
<script type="text/javascript">
function display(el) {
var id = $(el).attr('id');
alert(id);
}
</script>
<input id="btn" type="button" value="click" OnClick="display(this);" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With