Reputation: 33
Now code is this.
<button id=hine onclick=mo('ppp')>Go!</button>
<script>
function mo(name){
alert(name);
alert($(this).attr('id')); // undefined
}
</script>
How can I use $(this) in jQuery ?
Upvotes: 0
Views: 66
Reputation: 782508
You need to pass this
as an explicit argument.
<button id="hine" onclick="mo(this, 'ppp')">Go!</button>
<script>
function mo(element, name){
alert(name);
alert($(element).attr('id')); // undefined
}
</script>
Upvotes: 2
Reputation: 34147
Try this
function mo(elem, name){
alert(name);
alert($(elem).attr('id'));
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id=hine onclick="mo(this, 'ppp')">Go!</button>
Upvotes: 2