0

I am learning vueJS and have 2 clickable divs that share the same method. So when I click one of the divs the action happens for both of them.

What is the best way to action an @click on a div (This div) the one that is being clicked.

So I'm trying to open and close a menu only on the one (div) that is being clicked.

<div class="row">
  <div class="col-sm-3">
    <div class="mainbox-wrapper">
      <div class="mainbox">text</div>
      <div class="selectbox" @click="actionMenu" id="foo" ref="foo">select box</div>
      <div class="menu" v-show="isShow">select box</div>
    </div>
  </div>
  <div class="col-sm-3">
    <div class="mainbox-wrapper">
      <div class="mainbox">text</div>
      <div class="selectbox" @click="actionMenu" id="bar" ref="bar">select box</div>
      <div class="menu" v-show="isShow">select box</div>
    </div>
  </div>
</div>

js

  methods:{
    actionMenu(event){
      let targetId = event.currentTarget.id;
      this.isShow = !this.isShow;
    }
  }

2 Answers 2

1

Instead of v-show directive, you can toggle class for the relevant div:

actionMenu(event){
   event.target.nextElementSibling.classList.toggle('d-none');
}

d-none class is applicable for bootstrap 4. If you are using it, then it's great but if you're not using, then you can add your own class.

.d-none {
  display: none; /* bootstrap uses !important as well */
  /* if you wish, you can add !important */
}
Sign up to request clarification or add additional context in comments.

Comments

0

Rather use v-on than @click, you can learn more about HTML listeners here

Your HTML:

<div class="col-sm-3">
   <div class="mainbox-wrapper">
   <div class="mainbox">text</div>
      <div class="selectbox" v-on="click: onClick" id="foo" ref="foo">select box</div>         
  </div>
</div>

and in your js

methods: {
        onClick: function (e) {
          console.log(e.currentTarget.id;)
          }
    }

2 Comments

How do I bind this to the div or button which is being clicked though?
in the HTML you specify which method you want to call. in my example, v-on="click: <anyMethodName> "

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.