perhaps an odd request but i need ALL (not just one id) p elements to have spans.
so on document load…
<p>text</p>
would become…
<p><span>text</span></p>
any help would be incredibly appreciated!
perhaps an odd request but i need ALL (not just one id) p elements to have spans.
so on document load…
<p>text</p>
would become…
<p><span>text</span></p>
any help would be incredibly appreciated!
Load the script from the end of the body
<html>
<head>
...
</head>
<body>
...
<script type="text/javascript" src="js/script.js"></script>
</body>
</html>
where the script retrieves all p elements, moves their contents to a span, and then moves the span back into the p element
js/script.js
var els = document.getElementsByTagName('p'),
p,
span,
i;
for (i = 0; i < els.length; i += 1) {
p = els[i];
span = document.createElement('span');
while (p.hasChildNodes()) {
span.appendChild(p.firstChild);
}
p.appendChild(span);
}
Why do that with JavaScript? Sounds like a DOM-related homework question to me.