Skip to content Skip to sidebar Skip to footer

How To Pass A Javascript Function In A Dynamically Created Div

I want to pass a javascript function to a dynamically created div, like I am attempting below. If I replace the script string with an ordinary string this works, but is there a way

Solution 1:

A <script> tag is not a text node. You need to create a <script> element using document.createElement.

However, you should not do this, unless you're trying to load an external script file dynamically (eg, JSONP)

Solution 2:

var js=document.createElement('script');
  js.setAttribute("type","text/javascript");
  js.appendChild(document.createTextNode(" var pager = new Pager(3); pager.init(); pager.showPageNav('pager', 'pageNavPosition'); pager.showPage(1); "));
  var newDiv=document.createElement("div");
  newDiv.appendChild(js);
  document.appendChild(newDiv);

Post a Comment for "How To Pass A Javascript Function In A Dynamically Created Div"