Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VueJS Read Dom Attributes

I want to get the href attribute to the button click event.

<a v-on:click.prevent="func($event)" href="/user/all/2">
    <i class="fa fa-edit"></i>
    <span>Get Data</span>
</a>

Main.JS Files

new Vue({
el: 'body',

methods: {
    func: function (event) {
        element = event.target;

        console.log(element); // Output : Select span|i|a element

        href = element.getAttribute('href');
    },
}
});

Target event does not select a element. It selects the clicked element.

like image 593
Ramazan APAYDIN Avatar asked Sep 06 '16 20:09

Ramazan APAYDIN


People also ask

How do you access DOM elements in Vue?

If a ref attribute is added to an HTML element in your Vue template, you'll then be able to reference that element or even a child element in your Vue instance. You can also access the DOM element directly; it is a read-only attribute and returns an object.

What is REF () in Vue?

ref is a special attribute, similar to the key attribute discussed in the v-for chapter. It allows us to obtain a direct reference to a specific DOM element or child component instance after it's mounted.

How do I reference an element in Vue?

Referencing DOM Elements in Vue # If we want to reference a DOM element, we can use the ref attribute in Vue. Vue can store references to DOM elements in a property called $ref . The first thing we have to do, is add a ref attribute to the element we want to reference in our Javascript.


1 Answers

You want event.currentTarget, not event.target. Here's a fiddle of the situation: https://jsfiddle.net/crswll/553jtefh/

like image 147
Bill Criswell Avatar answered Sep 20 '22 12:09

Bill Criswell