javascript - Cannot insert html after placing less-than signs in it -


i have site need generate form fieldset template , save inside hidden element. when user needs add more fieldsets form, template read , inserted bottom of existing form. make sure template doesn't interfere jquery selectors i've replaced less-than signs , quotes %lt!% , %qot!% respectively, , idea was replace these proper signs again when template being inserted, reason isn't working.

to show situation in code:

html:

<div id="test">     %lt!%span id=%qot!%testing%qot!%>hello%lt!%/span> </div>  <span id="target"></span> 

js:

htmlstring = $("#test").html(); htmlstring = htmlstring.replace(/%lt!%/g, '<'); htmlstring = htmlstring.replace(/%qot!%/g, '"'); $("#target").before(htmlstring); 

it won't work long second line has less-than sign. placing other character there instead works fine, absolutely nothing gets added page. what's going on here?

jsfiddle example

to simplify things should use proper html encoded entities. rather hiding div can store template in data attribute.

<!-- language: lang-js -->     <span id="target" data-template="&lt;a href=&quot;#test&quot;&gt;test&lt;/a&gt;"></span>  <script>     htmlstring = $("#target").data('template');      $("#target").before(htmlstring); </script> 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -