Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass parameter to click event in Jquery

Tags:

jquery

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)" />
like image 1000
Acubi Avatar asked Sep 08 '11 15:09

Acubi


People also ask

How to pass parameter to click function in jQuery?

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')); .


1 Answers

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);" />
like image 68
jmar777 Avatar answered Oct 05 '22 17:10

jmar777