link is not called

Asked

Viewed 106 times

7

I have a code jQuery that generates a div it works smoothly, in it I have the following excerpt

div += "<p>Coloque o seu <a class='text-orange' id='link_new_ad'>veículo a disposição</a>!</p>";

then I add this div and it appears on the right screen, but my problem is that I wanted to call the event another button by clicking this link

$("#link_new_ad").click(function (){
    alert('teste');
    $("#bundle_ad_submit").trigger('click');
});

however when I click on the link nothing happens, if you put the link in html instead of put by append(div) it calls the event.

Somebody knows what’s going on, it’s better to fix it?

5 answers

4

Use $(seuelemento).on("click", function() {})

This is because the DOM has already been loaded, you need the event ON to listen to page changes.

  • Bro may believe what you said, but for some reason it didn’t work out the @pedro-camara-junior response worked.

  • Rodrigo, I did a test with ondirectly in the browser console but could not make it work, can post an example maybe?

  • 2

    @Rodrigo, you’re applying the event on() in an element that does not yet exist, this will not monitor the inclusion of the same.

  • There is another syntax, try $('body'). on("click", "your element", Function() {})

  • http://api.jquery.com/on/

  • Using your JS before or after the body can also influence... It is recommended to use JS before closing the body.

  • @Rodrigomello, this was a recommendation given by Yahoo in the early days of the internet. Today it is interesting to use <src="file.js" script />

Show 2 more comments

3


This is because you are not assigning the event click element before it exists in HTML.

As a solution you should create the click event right after adding the div to the HTML.

$(document).ready(function() {
  var html = "<p>Coloque o seu <a class='text-orange' id='link_new_ad'>veículo a disposição</a>!</p>";
  $('body').html(html);
  //Após adicionar no HTML atribua o evento ao link.
  $("#link_new_ad").click(function() {
    console.log('teste');
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

  • But then I add the div to a function after an ajax asynchronous call. You’re telling to add the click event inside that function?

  • You bet it worked, thanks guy I’ll put your answer as the right one.

  • That’s right. Tried using the onquoted by @Rodrigomello?

  • tried but it didn’t work, but then when I put the call inside the function there it worked.

  • @Marciusleandro In your case I think I’d better use the on in the manner quoted by @Tobymosque

1

Buddy, you can call the method . direct click() you don’t need Trigger.

$("#link_new_ad").click(function(){
    alert('teste');
    $("#bundle_ad_submit").click();
});

If it doesn’t work, check if $("#bundle_ad_submit") does exist, if it can find that object and if it has an event attached to it.

1

As @Pedrocamarajunior pointed out, you are trying to associate an event before it exists, in this case the ideal is to use the method on() in any relative closer to this element and pass a selector of this element as parameter.

Below is a short example of how to dynamically add content to the page and not have problems with the event bind.

var tmplCarro = document.getElementById("tmplCarro").content;
var container = $("#container");
var addCarro = $("#addCarro");

addCarro.on("click", function () {
  var carro = $(document.importNode(tmplCarro, true));
  container.append(carro);
});

container.on("click", ".enviar", function (event) {
  var enviar = $(this);
  var linha = enviar.parent(".linha");
  var carro = $(".carro", linha);
  alert("Enviando Carro: " + carro.val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<template id="tmplCarro">
  <div class="linha">
    <label> Carro:
      <input class="carro" type="text" />
    </label>
    <input class="enviar" type="button" value="Enviar" />
  </div>
</template>

<input id="addCarro" type="button" value="Adicionar Carro" />
<div id="container">

</div>

You can even use $(document).on("click", #link_new_ad", function () { ... }) which will work, but the recommended is to use the parent closer than document

0

I would use the following excerpt:

$("#link_new_ad").on('click', '#bundle_ad_submit', function()
{
     alert('teste');
})

Why so the event on, "would hear" the GIFT and see when it was born.

Browser other questions tagged

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