2

Looking to do something very similar to this: Get the calling element with vue.js

But I want to do it from a binding on the class. Is this possible?

Try to get element or event: https://jsfiddle.net/mrxunvyw/

new Vue({
    el: "#app",
    methods: {
      testFunction: function(event) {
      	console.log(event);
        return true;
      }
    }
});
.title{
  font-size: 24pt;
}
<script src="https://vuejs.org/js/vue.js"></script>

  <div id="app">
    <div :class="{title: testFunction()}">Test1</div>
    <div :class="{title: testFunction()}">Test2</div>
    <div :class="{title: testFunction()}">Test3</div>
    <div :class="{title: testFunction()}">Test4</div>
  </div>

6
  • This doesn't seem possible. There isn't event data here to get an event target, and $refs is not yet set when the class binding is evaluated. Commented Sep 29, 2018 at 5:25
  • What were you planning to do with the element reference? Maybe there's another way of achieving that goal. Commented Sep 29, 2018 at 5:27
  • @tony19 basically I have a form, but I am trying to get the parent element of the form elements. The form elements are broken into multiple groups. This would be the form: stackoverflow.com/questions/52371994/…. It really just comes down to making it more dynamic and easier to maintain. I have work arounds, but it might be a little difficult to maintain. Commented Sep 29, 2018 at 5:32
  • It's still unclear to me why the element reference is needed. What do you intend to do with the reference? Commented Sep 29, 2018 at 6:16
  • @tony19 Set variables, set classes, etc... Basically, I would like to know what the parent element is, so that I can set a variable based off of that. As of right now, I have 5 different methods, with slightly different code. You can see this off the link I posted before. The answer does what I want, but it would be nice to know what group of form elements I'm on, this way I can just create an array of options and reset everything except for the selected element grouping. Again, more about cleaning up and maintenance than anything. Commented Sep 29, 2018 at 6:53

0

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.