-1

When I use Jquery to get a object from current target it's work fine.

But same behavior I would like to achieve by vueJs but VueJs returns a dom not a object.

Using Jquery

    <a onclick="test(this)" type="button">Button</a>

    function test(e){
      console.log($(e));
    })

Result :

init [a.cursor_pointer, context: a.cursor_pointer]
    0: a.cursor_pointer
    context: a.cursor_pointer
    length: 1
    __proto__: Object(0)

Using VueJs

<a @click="test" type="button">Read more</a>

 methods:{
    test(e){
       console.log(e.target);
    }
}

Result :

<a type="button" class="cursor_pointer">Read more</a>

1 Answers1

0

TL/DR; For what you are trying to do, there is no 1:1 jQuery to Vue equivalent.

It looks like you're logging jQuery's event this (the <a> element, in this context), coerced into a jQuery [prototype] object, in your jQuery example.

The e.target in your Vue example is equivalent to the unadorned e in your jQuery click handler.

By wrapping e in $() in test(), you are imbuing the <a> with the jQuery prototype (so it has access to the jQuery API and all of its methods).

There is no direct equivalent to the jQuery prototype in Vue but you can:

  1. Include jQuery IN your Vue app
  2. or, use equivalent vanilla JavaScript on the e.target to accomplish something similar. http://youmightnotneedjquery.com/ is a pretty good resource if you are not comfortable digging through the jQuery Source

EDIT In light of your clarification (which you should edit into your original question), it looks like you have encountered the key difference between how one approaches a problem with jQuery versus the approach in frameworks such as Vue, React, Angular (et cetera). That is to say, the difference between imperative and declarative programming. Here is a good description of that difference.

To do what you want in the "Vue way" you might create a component for your "review" blocks, with each review handling its "Read More/Read Less" state and toggling accordingly.

You could accomplish what you want in a Vue application using vanilla JavaScript or by including jQuery and adding your click listener in the mounted lifecycle hook. While not wrong, it isn't taking advantage of Vue's declarative-ness.

tony19
  • 125,647
  • 18
  • 229
  • 307
Ito Pizarro
  • 1,607
  • 1
  • 11
  • 21
  • When i use `console.log( e.target )` in Jquery nothing to show in console. I would like to achieve same result like Jquery `(console.log($(e))` in vueJs – Abdullah Al Mamun Apr 18 '19 at 15:23
  • Vue won't give you the jQuery prototype that you are getting by wrapping the `e` in `$()` without also including jQuery in your build. Are you doing something with the `$(e)` that relies on jQuery [prototype] methods? – Ito Pizarro Apr 18 '19 at 15:33