0

My view is like this :

...
 <td  @click="modalShow('modal-option', '{{ $i }}')">
    ...
</td>
...

@section('modal')
    <option-modal id="modal-option" :id="idModal"></option-modal>
@endsection

When the view clicked, it will display modal like this :

<template>
    <div class="modal" tabindex="-1" role="dialog">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                ...
                <div class="modal-body">
                    <a href="javascript:" class="text-danger" :id="'image-delete-' + id">
                        <span class="fa fa-trash"></span>
                    </a>
                </div>
                ...
            </div>
        </div>
    </div>
</template>

<script>
    export default{
        name: 'OptionModal',
        props:['id'],
    }
</script>

I display modal with vue.js 2

When I click icon trash, I want to call javascript

I have global javascript. The name is main.js

The main.js is like this :

for(let i = 0; i < 5; i++) {   
    $('#image-delete-'+i).click(function(){
        $('input[name="image-'+i+'"]').val('');
        document.getElementById("image-view-"+i).style.display = "none";
        document.getElementById("image-upload-"+i).style.display = "";
        $('.modal').modal('hide');
    });
}

When I clicked the trash icon in modal, javascript was unsuccessful called

How can I solve this problem?

11
  • why do u use interpolation inside the @click ? you get there full access to your methods and data. Commented May 30, 2017 at 7:37
  • You must $emit (raise) an event from option-modal and grab it on your view Commented May 30, 2017 at 7:38
  • @Max, I am confused. You should answer with the code Commented May 30, 2017 at 7:40
  • @LiranC, It does not matter Commented May 30, 2017 at 7:42
  • Vue2 Events Commented May 30, 2017 at 8:14

1 Answer 1

1

Whether the click handlers you are adding via jQuery will work depends entirely on when you add the click handlers. If you are adding the click handlers before the Vue is mounted, then your clicks will never fire.

This won't work

for(let i = 0; i < 5; i++) {   
  $('#image-delete-'+i).click(function(){
    ...
  });
}

new Vue(...)

Here is an example.

If you add your handlers in mounted, then you can get it to work.

This should work

new Vue({
  el:"#app",
  data:{
  },
  mounted(){
    for(let i = 0; i < 5; i++) {   
      $('#image-delete-'+i).click(function(){
        ...
      });
    }
  }
})

Example.

Finally, you could add your handlers in this fashion and it wouldn't matter when you added them.

for(let i = 0; i < 5; i++) {   
  $(document).on("click", `#image-delete-${i}`, function(){
    ...
  });
}

But, I'm not sure this is what is happening because you give so little code. Of course the ideal case would be to let Vue define the handlers.

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

2 Comments

Okay, Thanks. I will try it first
Yes. Thanks for your help

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.