How to activate onclick after a jquery?

Asked

Viewed 589 times

1

I have some botões which are mounted dynamically via jquery, Turns out on the front page I have some events like that botões, but as they do not yet exist at the time of page loading the events are not loaded, I needed to do something like a refresh in those events so the jquery were loaded.

Code:

Abre Pagina HTML
$(".meusbtns").click(function...);
Monta HTML Basico
Clica numa lista e essa lista traz o result por um jquery
<button class="meusbtns" value="x">Botao</button>

Notice that the class meusbtns exite, but as she came by jquery the event was not initially loaded.

I know there is a way to add and remove classes, which would reset the event, but since these buttons are dynamic I can’t imagine how to do it this way.

I hope I was clear

  • 1

    There are several questions about this... one of them is: http://answall.com/a/36812/129

  • Another interesting question for this problem is http://answall.com/q/5196/129

2 answers

5

Here is another question on the same subject:

How to ensure that an element will have the DOM event?

Use outside the pageLoad:

$(document).on("click", ".meusbtns", function(){
    /*Seu Código*/
});

The function .on("click"): can work dynamically with elements that were created later on the page.

0

Simple, you have to bind these click events after mounting the buttons.

Example:

function montaView() {
  ... // supondo que você tenha montado seus botoes
  bindEventos();
}

function bindEventos() {
  ... // faz o bind dos eventos
}

Heed!

If you call the bind event on an element that already has your Inds, it will bind again and the event will run x times you made the bind. To avoid this you can use the .off() to remove the previously binded event and redo the bind.

Example:

botoesNaoDinamicos.off('click');
todosOsBotoes.click(...);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.