Reputation:
I have a bunch of elements on a page that are formatted like below:
<div class="longdesc">
<pre style="...">
//stuff here
</pre>
</div>
<div class="longdesc">
<pre style="...">
//stuff here
</pre>
</div>
I'm trying to replace some of the content inside the <pre>
tag but I'm having trouble.
(function () {
var nodes = document.getElementsByClassName("longdesc");
for (var n=0; n<nodes.length; n++) {
var node = nodes[n].getElementsByName("pre");
node[0].textContent = node[0].textContent.replace("<", "<");
//other code
}
})();
VM5185:4 Uncaught TypeError: nodes[n].getElementsByName is not a function
I only need the first pre
in each longdesc
. How can I do this?
Upvotes: 1
Views: 15633
Reputation: 177
Try using getElementsByTagName()
instead.
http://www.w3schools.com/jsref/met_element_getelementsbytagname.asp
Upvotes: 1
Reputation: 467
try this:
(function () {
var nodes = document.getElementsByClassName("longdesc");
for (var n=0; n<nodes.length; n++) {
var node = nodes[n].getElementsByTagName("pre");
node[0].textContent = node[0].textContent.replace("<", "<");
//other code
}
})();
Upvotes: 2