I have a <script> tag with content within a <div>element. Now I need to copy / clone the content, store it in a variable and append it later.
Strangely when I append the variable content, I just get the outer <div>.
Any idea what I'm doing wrong? We are using jQuery 1.8.2.
Here is my code:
html content:
<div id="payl">
<div class="toolTipHandler">
<script type="text/javascript">
var xxx_title = "<h4>title</h4>";
var xxx_text = "<p>bla bla</p>";
</script>
</div>
</div>
script.js content:
var toolTipHandler = jQuery('#payl .toolTipHandler').clone(true);
document.getElementById('payl').innerHTML = '';
jQuery('#payl').append(toolTipHandler);
Result:
<div class="toolTipHandler"></div>
Having a script tag within a div doesn't assign the variables within the script to the div. That script tag can live anywhere in the page. Or the variables might as well be declared in your script.js file.
Maybe a better approach would be to use data- attributes in the toolTipHandler div:
But putting markup in the values isn't really good use of the data- attributes. I would do something like this:
Then insert the values from toolTipHandler data. Your JavaScript might look something like this: