ejecutando js desde dentro del contenido popover de arranque

Inside a bootstrap 2.3 popover I display some html code. And I need to trigger a js function when I click on it but I have an hard time to do so.

The js function is properly executed if I click on the div that contains the HTML that is rendered inside the popover but not if I click the content inside the popover itself.

Here my coffee

  html: true
  content: ->

$(".updateit").on "click", ->
    console.log "pippo"

Y el html

<a data-placement="right" data-toggle="popover" href="#">click me</a>
<div id="content">
  <div class="updateit" style="color:red">abc</div>

For testing purpose div#content is not hidden and if I click it, "pippo" is printed out on console, however if I click on the popover content (the html looks the same) nothing is printed out on console.

Is there a way to call js function from inside the popover content?


preguntado el 27 de noviembre de 13 a las 06:11

1 Respuestas

This looks like a common jQuery event binding gotcha. What's probably happening is $(".updateit").on "click", () -> ... is running before the popover is created. That particular function will only bind a click event to existing elements; new ones won't have the event applied. Since the popover will create its own element with the 'updateit' class after being run, the event won't apply to it.

Fortunately, the fix is pretty easy. $(document).on 'click', '.updateit', () -> .. should do the trick. This will apply to every element of class 'updateit' within the document, regardless of when it's created.

Relevant jsbin - http://jsbin.com/ALESUXib/1/edit

respondido 27 nov., 13:06

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.