cobber
cobber

Reputation: 38

multi text node manipulation with jQuery

If I have the following html structure:

<ol>
<li>
    Text A <span>Other text</span>, Text B
</li>
<li>
    Text A <span>Other text</span>, Text B
</li>
</ol>

How do I iterate through each list item using jQuery and change the text for both Text A and Text B individually without disturbing the span and it's contents?

Upvotes: 0

Views: 53

Answers (1)

Jose Ricardo Bustos M.
Jose Ricardo Bustos M.

Reputation: 8164

$("ol li").contents().filter(function() { 
  return this.nodeType === 3;}
);

in other words fiddle:

$("ol li").contents().filter(function() { 
  return this.nodeType === 3;}
).each(function(i, obj){
  //your code using i, obj or this for choose
  this.textContent = "change";
});

Upvotes: 1

Related Questions