pegya
pegya

Reputation: 33

I can’t use $(this) Jquery

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

Answers (2)

Barmar
Barmar

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

kiranvj
kiranvj

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

Related Questions