Appendto(<script></script>) does not execute the added script

Asked

Viewed 40 times

1

I’m creating a light box so it runs based on clicking a link.

The light box works normally and I can add content inside it without problems. The point is that in this one I need to add a javascript that pulls one of my forms.

Javascript is even printed inside the DIV but the browser doesn’t run it. Can anyone tell me what I can do to fix this?

Follow the code I’m using below.

jQuery(function ($) {
  function createLightBoxForm() {
    var linkForm = $('.pega-form').attr('value');
    var bgLight = $('<div class="bg-lightbox"></div>');
    var colorLight = $('<div class="color-lightbox"></div>').appendTo(bgLight);
    var contLight = $('<div class="cont-lightbox"></div>').appendTo(bgLight);
    var closeLight = $('<a class="close-btn-lightbox" href="#">X</a>').appendTo(contLight);
    var formLight = $('<script type="text/javascript" src="//go.secnet.com.br/form/generate.js?id=' + linkForm + '"><\/script>').appendTo(contLight);
    bgLight.appendTo('body');
  }
  $('body').on('click', '.ltbox-call', function(event) {
  	event.preventDefault();
  	createLightBoxForm();
  });
  $('body').on('click', '.close-btn-lightbox', function(event) {
    event.preventDefault();
    $('.bg-lightbox').remove();
  });
  $('body').on('click', '.color-lightbox', function() {
    $('.bg-lightbox').remove();
  });
});

Upshot:

inserir a descrição da imagem aqui

  • Hello Luan, I want to help you but you have provided little information to understand the origin of the problem. But superficially it could say that you are having problems accessing the script loaded dynamically, due to the issue of order in the case, the script that is "appended" must be ready to be used, in case see if such a script has some method "load" to be executed after being loaded. or provide more information to help you

  • Says Vinicius, now that you have commented on the load already gave me a light, I will check this straight and if still can not, I bring more information to you. Many thanks!

No answers

Browser other questions tagged

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