1

I'm trying to add a script tag as value of a variable within a script. that is ..

<script>
    $(document).ready(function() {
        var iCnt = 0;
        $('#btAdd').click(function() {
            if (iCnt <= 19) {
                iCnt = iCnt + 1;
                var div = '<div id="node' + iCnt + '" class="item">'+ iCnt +'</div>';
                var jsplmb = '<script> jsPlumb.ready(function() { addPlumb("node'+ iCnt +'") });</script>';
                $('#diagramContainer').after(div);
            }
        });
 });

</script>

which is not working. The close script tag in the variable jsplumb act as the close tag of main script tag.

also the dynamic addition of var div is not adding to

<div id="diagramContainer"> </div>
Nisfan
  • 149
  • 3
  • 15
  • also the dynamic addition of html components is not working. pls give a solution for that too.. – Nisfan May 22 '15 at 07:15
  • BTW, your `jsplmb` does nothing and is never used. – Drakes May 22 '15 at 07:20
  • what about `div` .. its not working (the html elements are not adding ) – Nisfan May 22 '15 at 07:22
  • Seems to be working: http://jsbin.com/ruzumu/2/watch?html,js,output Unless you have the wrong selector `#diagramContainer` – Drakes May 22 '15 at 07:28
  • its showing an warning message : `Use of getPreventDefault() is deprecated. Use defaultPrevented instead`. Is the problem . Any way its not working even after coping your entire code. But its work in `JS Bin` – Nisfan May 22 '15 at 07:40
  • Sounds like you have more code than this snippet. I suggest you ask a new questions minus the `jsplmb` stuff. – Drakes May 22 '15 at 07:50

1 Answers1

2

The script tags are parsed before the code inside them, so the browser doesn't know that you intended the closing script tag to be a string in the code.

You can break up the closing script tag into separate strings:

var jsplmb = '<script> jsPlumb.ready(function() { addPlumb("node'+ iCnt +'") });</scr' + 'ipt>';
Guffa
  • 687,336
  • 108
  • 737
  • 1,005