2

I'm working in VueJS. I'm trying to bind a class to one element based on an existence of a class on another element. The below is in a :for loop to print out the list.

The '#accordion-'+(index+1)) is the id of the div I want to check to see if a class exists on it.

I wrote a method and it works UNTIL I check the element's classList. Right now, I'm only doing a console log, but eventually this will return true and hopefully the class will apply.

methods: {
  existingTopic: function(lcDivID) {
    const element = document.querySelector(lcDivID);
    console.log(element); //It gives me the element.
    /* The below is where it crashes */
    console.log(element.classList.contains("collapsePanelExistingTopic"));
  }
}

I find it so frustrating. I've spent a day on this without any results. Any help you can provide it would be great.

2
  • When it crashes, what error do you see? Commented Feb 4, 2020 at 20:08
  • I get a "Cannot read property 'classList' of null Commented Feb 4, 2020 at 22:09

1 Answer 1

1

Here it is, you can also use this.$el as document

...
methods: {
    hasClass() {
      const element = this.$el.querySelector('h1')
      if (element.classList.contains('your-class-here')) {
        console.log(true)
        this.$el.querySelector('anotherelement').classList.add("your-another-class");
      } else {
        console.log(false)
    }
  }
},
mounted() {
  this.hasClass()
}
...

Alternative

<h1 class="mb-5 fs-lg" ref="myElement">Sign up</h1>
...
methods: {
  hasClass() {
    const element = this.$refs.myElement
    if (element.classList.contains('mb-5')) {
      console.log(true)
      this.$el.querySelector('anotherelement').classList.add("your-another-class");
    } else {
      console.log(false)
    }
  }
},
mounted() {
  this.hasClass()
}
...

So you can define ref as :ref="index+1" in your loop

Sign up to request clarification or add additional context in comments.

3 Comments

OK. I'm going to give this a shot. Thank you,,,I'll get back to you :-)
I apologize. I have a really stupid question. How would I call up hasClass()? Can I use this on a v-bind:class on another element? This is my "existingTopic" in use below. <div id="timeList" class="timeListDiv" :id="'tTimeDiv'+(index+1)" v-bind:class="{ieTimeList: existingTopic('#accordion-'+(index+1))}"> would I do... <div id="timeList" class="timeListDiv" :id="'tTimeDiv'+(index+1)" v-bind:class="{ieTimeList: hasClass()}">
Hmmm... it's not working. I'm trying to bind a class on one element based on another having the class. Because it's dynamic, I have to check within it's own index. Thus I'm trying to call up the function on the div I want to set the class based on if it exists on another div with an id of #accordion- + the index number +1. Ugh. <div id="timeList" class="timeListDiv" :id="'tTimeDiv'+(index+1)" v-bind:class="{ieTimeList: existingTopic('#accordion-'+(index+1))}">

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.