1

I'm trying to get the value of an element when a user click on it. This is my code:

<div id="heroes-selection" class="row dflex justify-content-left">
    <figure id="luna" class="heroes-pic border text-danger m-3" value="heroes.luna">
      <img class="animated-gif">
   </figure>

   <figure id="qop" class="heroes-pic border text-danger m-3" value="heroes.qop">
      <img class="animated-gif">
   </figure>
</div>

I've tried to use Jquery $(this) and the result came out as undefined.

$(".heroes-pic").on("click", () => {
  player = $(this).attr("value");
  console.log(player);
});

My goal is to get the value heroes.luna when the user click on the figure with id="luna". The same for id="qop", I need to get the value heroes.qop. Thank you!

1
  • Just change arrow function ()=> to normal function(). Commented May 13, 2018 at 8:44

2 Answers 2

1

You have to use the classic function instead of arrow function. this on arrow function refers to the HTMLDocument and on the object clicked. And that is the reason why you are not getting the correct result. You are getting attr value from HTMLDocument.

$(".heroes-pic").on("click", function() {
  player = $(this).attr("value");
  console.log(player);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="heroes-selection" class="row dflex justify-content-left">
  <figure id="luna" class="heroes-pic border text-danger m-3" value="heroes.luna"> heroes.luna
    <img class="animated-gif">
  </figure>

  <figure id="qop" class="heroes-pic border text-danger m-3" value="heroes.qop"> heroes.qop
    <img class="animated-gif">
  </figure>

</div>


Other option: you can use event.currentTarget instead of this on arrow function

Like:

$(".heroes-pic").on("click", (event) => {
  player = $(event.currentTarget).attr("value");
  console.log(player);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="heroes-selection" class="row dflex justify-content-left">
  <figure id="luna" class="heroes-pic border text-danger m-3" value="heroes.luna">
    <img class="animated-gif">heroes.luna
  </figure>

  <figure id="qop" class="heroes-pic border text-danger m-3" value="heroes.qop">
    <img class="animated-gif">heroes.qop
  </figure>

</div>

Sign up to request clarification or add additional context in comments.

Comments

1

This is because you are using an arrow function that binds the this parameter to the enclosing scope's this.

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#No_separate_this

replace it with a function:

$(".heroes-pic").on("click", function () {
  player = $(this).attr("value");
  console.log(player);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.