0

I would like to have attribute tblid from button element, when click event is triggered.

I was reading questions like this, but it's not working for me -> example below.

What seems so strange to me is that e.target or e.currentTarget returns the element with all attributes, including tblid. Why is so hard in vuejs to get those attributes from element?

Please note: I know I can create component and use props on that component. But I really don't want to create component for every html element like button, label, input ...

html:

<div id="app">
      <button tblid="tbl1" @click="buttonClick($event)">
        Button 1 tbl1
      </button>
    </div>

app.js:

new Vue({
  el: "#app",
  methods: {
      buttonClick(e){
          debugger;
      }
  }
});

enter image description here

1
  • Vue does not blindly pollute the DOM. Adding every single attribute of a VNode element to the DOM element would decrease the performance of the DOM considerably. Commented Dec 10, 2019 at 9:18

1 Answer 1

1

If you want the value of tblid you could just send that as parameter in the even listener function instead of using the event object.

<button tblid="tbl1" @click="buttonClick('tbl1')">
    Button 1 tbl1
</button>
Sign up to request clarification or add additional context in comments.

4 Comments

Getting attribute is not possible?
If you really want to you could try by adding a data-attribute somethine like data-tblid and in the elements object you can access like evt.target.dataset.tblid
well I think adding the parameter directly to the event listener might be more vue style, both as long as both methods work its okay to use whichever you prefer I guess.
Thanks for answers. You were very helpful - I've also discovered that I can use e.currentTarget.getAttribute('attr_name').

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.