0

I have created in my vue component the following template

                <b-button v-on:click="check($event)" class="btn btn-success" data-action="consent">
                    <b-icon-check></b-icon-check>
                </b-button>

However the click event only seems to work if I click on the button itself, for example on the sides around the icon, and not when clicking on the icon itself.

I'm wondering if there's a better way to do what I want to achieve instead of adding another v-on:click on the icon itself.

Basically I want the button to trigger the click event even if any subelement it has is clicked.

enter image description here

Here's the function check(event)

    methods:{
        check(event){
            let action = event.target.getAttribute('data-action');
            if(action === 'consent'){
                this.checked = true;
            }
            else{
                this.checked = false;
            }
        }
    },

After reading the comments and the link for modifiers, I think my mistake is assuming that the event.target is going to be the button. Instead it's the icon itself that's why my code is failing.

7
  • It looks like something is stopping the icon from bubbling to the button, can you share the code of the check method? Commented Mar 20, 2020 at 10:01
  • Test the modifiers of v-on: vuejs.org/v2/guide/events.html#Event-Modifiers Commented Mar 20, 2020 at 10:03
  • I just updated the question because I think the event is actually triggering but doesn't work because I'm working on the wrong event.target. But I'm wondering why it doesn't propagate to the button as well. Commented Mar 20, 2020 at 10:11
  • I've tested your code, and it did trigger the event when click on the icon Commented Mar 20, 2020 at 10:14
  • you missed data-action for the icon which fails event.target.getAttribute('data-action') add data-action="consent" to the <b-icon-check></b-icon-check> Commented Mar 20, 2020 at 10:17

1 Answer 1

3

Use event.currentTarget.getAttribute('data-action') to grab the data* attribute from where the event listener is bound. event.currentTarget will always be the element the event listener is bound to (rather than the element that actually initiated the click event, which is what event.target points to).

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

2 Comments

I'll definitely try that and get back to you :)
Yep it was perfect. That's what I needed thank you very much

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.