0

I am trying to get all images for a category using Vue

 <div class="col-md-12 col-sm-2 p-2">
  <a v-on:click="onCategoryManageImageClick($event)"  data-target="#location- 
  category-images">
 </span>
  </a>
 </div>

So the event onCategoryManageImageClick ($event) does not work, if I am adding a html block and then click on get image button.

this is index.js

      methods:{
        onImagesTabClick(){
        this.$root.$emit('activated-tab:location-images');
    },
    onCategoriesTabClick(){
        window.j1App.eventBus.$emit("j1-location-image-list:shown");
    },
    onCategoryManageImageClick: function(event) {console.log('working event..');
        event.preventDefault();
        window.j1App.eventBus.$emit("j1-location-category-image- 
 list:shown",event.currentTarget.id);
    }
}

So basically it need to to work like we do in jQuery

$(document).on('click',function{
   })

So it works either page load or if adding any new html element on DOM. same I want in Vue.

3
  • How do you add the HTML block? It does not show it the vue template, which would be the obvious way to do it. Commented Aug 20, 2020 at 9:32
  • I am adding html block using twig template, and then that block has the v-on:clcik atatched Commented Aug 20, 2020 at 9:34
  • <div class="col-md-12 col-sm-2 p-2"> <a v-on:click="onCategoryManageImageClick($event)" data-target="#location- category-images"> </a> </div> this is the html block get added once user add more button Commented Aug 20, 2020 at 9:42

2 Answers 2

1

You cannot alter the Vue template outside of Vue. That won't work. Vue compiles the template once when starting up and adds the event listeners to the rendered elements. If you add elements afterwards, Vue will not know about them.

The correct way of doing this would be to add those new elements in Vue.

<div 
  class="col-md-12 col-sm-2 p-2"
  v-for="item in items" 
  :key="item.id"
>
  <a 
     v-on:click="onCategoryManageImageClick($event, item)"  
     data-target="#location-category-images"
  >
  </a>
</div>

See https://v2.vuejs.org/v2/guide/list.html for documentation. In this case you need the items array variable in data and add more array elements to it, if you need more links.

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

1 Comment

But I am using vue with twig templates, So I wonder how its works on page laod?
0

Try raplacing your a tag with p

 <div class="col-md-12 col-sm-2 p-2">
  <p v-on:click="onCategoryManageImageClick"  data-target="#location- 
  category-images">
  </p>
 </div>

7 Comments

OK let me try that
ReferenceError: item is not defined
Hey, sorry that I got lost in stuff. Just use p tag and event like this v-on:click="onCategoryManageImageClick". It will work for sure.
and I need id of current clicked button so used onCategoryManageImageClick($event) if you say to sue it just onCategoryManageImageClick then how can I get current clicked button ID ?
You can access the event inside method without passing it as function($event){}. Here is a pen that i created for reference. codepen.io/lambrohan/pen/oNxzRaN
|

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.