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

Asked

Viewed 94 times

1

Good afternoon,

Today I am facing a rather strange bug in my application:

I have a textarea field where I type a body of email and just below it a button that when clicked obtains the content of the textarea, opens a modal and inserts the same content(value) inside the body of the modal.

The point is that some users are reporting that the button opens the modal but does not insert the value of the textarea within it.

While analyzing this issue I identified that it is not always that this occurs, when it gives problem, if you update the page the function tends to work.

When error occurs, when inspecting button click events I realize that it has been replaced by a Bootstrap JS function or simply no other function is replaced.

How can I make sure that doesn’t happen?

  $('#btnPreviewEmail').click(function(){
  var assunto  = $('#txtAssuntoEmail').val();
  var texto    = CKEDITOR.instances['txtAreaCorpoDoEmail'].getData();
  var namepage = 'Campanha Marketing Lojas Uno';
  var landing  = buscaLandingPorNamePage(namepage);
  assunto = assunto.replace(/{{.FirstName}}/g, "Renoir dos Reis");
  assunto = assunto.replace(/{{.DataHoje}}/g, '31/10/2016');
  texto   = texto.replace(/{{.FirstName}}/g, "Renoir dos Reis");
  texto   = texto.replace(/{{.Tracker}}/g, "");
  texto   = texto.replace(/{{.URL}}/g, landing);
  texto   = texto.replace(/{{.DataHoje}}/g, '31/10/2016');
  $('#containerAssunto').html(assunto);
  $('#containerPreview').html(texto);
});

Code of the button that opens the modal:

<button type="button" class="btn btn-warning" id="btnPreviewEmail" data-toggle="modal" data-target="#modalPreview">Pre-visualizar</button>

Modal:

inserir a descrição da imagem aqui

  • 1

    You can put the HTML and code that fetches the value of the textarea?

  • What is the line that fetches the value? is the text or the subject?

  • @Sergio will both be searched

  • But they both come from one texarea and have problems appearing in the modal?

  • @Sergio, txtAreaCorpoDoEmail is a textarea of CKEDITOR, and the subject comes from an input type="text". Both do not appear in modal.

  • 1

    And the code that actually opens the modal?

  • @bfavaretto entered the missing details.

  • In the browser console (F12) some error appears?

  • @bfavaretto no :(.

  • Everything indicates that you try to put content in the modal before the bootstrap leaves this modal ready. It may be that, it may not be. Try to put this code in a $('#modalPreview').on('show.bs.modal', function (event) { /* COD AQUI */ }) instead of putting on the button click

  • 1

    That button btnPreviewEmail is created dynamically? Tries to replace $('#btnPreviewEmail').click(function(){ for $('#btnPreviewEmail').on('click', function(){

Show 6 more comments

1 answer

4


Have you tried using the click event this way?

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

The code must be outside the Pageload of the script.

Difference between the .on("click") and .click() -

.on("click"):

  • It can work dynamically with elements that have been created from later on the page.
  • Only loaded when the event is triggered.
  • It should be used in a parent element, which is on the page since Pageload, to search from it the element that will be created dynamically (so $(document).on() and not $('#btnPreviewEmail').on()

.click()

  • It cannot be triggered if you have created an element later on your page.
  • It is loaded even if the element is not triggered.

More details: Reference in English

I hope I’ve helped.

  • Thales, if you could just explain the difference between this and the shortcut . click(). So someone who accesses this question can have a better response based on documentation.

  • Excellent work, but my handler was referenced after the creation of the element. Therefore, anyone would have to work.

Browser other questions tagged

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