Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access corresponding DOM element onClick() in v-repeat item #1181

Closed
timavo opened this issue Aug 19, 2015 · 5 comments
Closed

Access corresponding DOM element onClick() in v-repeat item #1181

timavo opened this issue Aug 19, 2015 · 5 comments

Comments

@timavo
Copy link

timavo commented Aug 19, 2015

I got a list of users which are displayed with v-repeat.

HTML:

<ul id="vue">
    <li v-repeat="user in users" v-on="click: onClick(user)">@{{ user.name }}</li>
</ul>

JavaScript:

new Vue({
    el: '#vue',

    data: {
        users: [
            { name: 'Berta' },
            { name: 'Delta' },
            { name: 'Anton' },
            { name: 'Echo' },
            { name: 'Charlie' }
        ]
    },

    methods: {
        onClick: function(user) {

            // Perform jQuery actions on DOM element like: 
            // if($(this).hasClass('active')) { ... }
            // $(this).effect('shake)';

        }
    }
});

How can I access the corresponding DOM element to the clicked user? I want to perform some jQuery actions...

Thanks a lot! :-)

@yyx990803
Copy link
Member

Please post questions to vuejs/Discussion.

v-on="onClick(user, $event) then in your function you can access event.target.

@timavo
Copy link
Author

timavo commented Aug 19, 2015

Merci beaucoup! And thanks for the tip with vuejs/Discussions!

@potasiyam
Copy link

potasiyam commented May 3, 2016

I am having an weird problem with event.target.
Suppose i have an anchor like this,
<a href="#p12" v-on:click="showDetails()">details</a>
when I click it the event.target is the anchor which is perfect.

But if I write the a tag with font-awesome like this,
<a href="#p12" v-on:click="showDetails()"><i class="fa fa-angle-down"></i></a>
then the event.target becomes the i tag from inside of a.

And again if I write like
<a href="#p12" v-on:click="showDetails()"><i class="fa fa-angle-down"></i> details</a>
everything is as it is supposed to be, event.target becomes the a tag.
You can check this with $(event.target)[0].nodeName
Not sure if this issue belongs to be this thread or a new thread.

@simplesmiler
Copy link
Member

simplesmiler commented May 3, 2016

Hello @potasiyam

This is a usage question, and we encourage you to ask it on forum or gitter. We try to use the issue tracker for bug reports and feature requests.

As for the question, use event.currentTarget when you need the node that has the listener is on.

@S-Amin
Copy link

S-Amin commented Jul 27, 2017

I hope solve someone problem. we can use the vue.$ref:

<ul id="vue">
    <li v-for="(user, i) in users"  
         v-on:click="onClick('u'+i)" 
         v-bind:ref="'u'+i">{{ user.name }}</li>
</ul>

and in your onClick function you can access it like this:

onClick: function(user) {
                  var element = this.$ref[user];
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants