user1532339
user1532339

Reputation: 45

nodeValue and filtered characters?

I'm trying to make a script to verify if a nodeValue is in the html document

var myVar= document.getElementsByTagName("b");
for (var i = 0; i < myVar.length; i++) {
  var pumpkin = myVar[i].firstChild;  
  if(pumpkin.nodeValue == "Some text")
  {
    alert("exists");
  }
}

It doesn't work if the text I want to find contains filtered characters like &nbsp; instead of spaces. When I'm looking with DOM Inspector those characters are not there (there are normal spaces instead), but when I look page source they are.

I tried

if(pumpkin.nodeValue == "Some text")

and

if(pumpkin.nodeValue == "Some&nbsp;text")

but both failed... Anyone have an idea?

(it works fine when the text I want to find doesn't contains these characters in the source code). thanks in advance

Upvotes: 1

Views: 157

Answers (1)

The Alpha
The Alpha

Reputation: 146239

You can do it with help of regular expression because &nbsp;'s corresponding character code is 160

var myVar= document.getElementsByTagName("b");
for (var i = 0; i < myVar.length; i++) {
    var str = myVar[i].firstChild.nodeValue;  
    var re = new RegExp(String.fromCharCode(160), "g");
    if(str.match(re)) alert("exists");
}

DEMO.

Upvotes: 0

Related Questions