1

Here is my line:

myArray[i].innerText == x

Here are the values from Chrome console:

myArray[i] = "13 ml Apollo glaze sherry truffel 250ml"

x = "13 ml Apollo glaze sherry truffel 250ml"

myArray[i].innerText == x returns false Why is this happening?

Here is how I define myArray:

myArray= $($('#myIframe').contents()).find('body#tinymce').find('p, li');
petko_stankoski
  • 10,459
  • 41
  • 127
  • 231

3 Answers3

3

debug it, you most likely have some extra whitespace characters in it.

console.log(escape(myArray[i].innerText));

You probably need to trim it.

Or the HTML entities are there.

var replacedString = myArray[i].innerText.replace(/ /g," ");

If you have a bunch of special characters, you will need to replace them all.

Community
  • 1
  • 1
epascarello
  • 204,599
  • 20
  • 195
  • 236
  • You were right. But I don't have extra space, but the first string has white space, and the second has "nbsp;". How to make the second string replace the nbsp with " " ? – petko_stankoski Oct 01 '12 at 12:57
  • Then the comparison returns false again. – petko_stankoski Oct 01 '12 at 13:00
  • escape(myArray[i].innerText) = "13%20ml%A0Apollo%20glaze%20sherry%20truffel%20250ml" escape(escape(selectedText.replace(/ /g," "))) = "13%20ml%20Apollo%20glaze%20sherry%20truffel%20250ml" – petko_stankoski Oct 01 '12 at 13:04
  • You do not want the escape in the actual comparison. Are you doing that? – epascarello Oct 01 '12 at 13:07
  • @escaparello No. myArray[i].innerText = "13 ml Apollo glaze sherry truffel 250ml" and x = "13 ml Apollo glaze sherry truffel 250ml" But before the comparison I make x.split(" ").join(' ') – petko_stankoski Oct 01 '12 at 13:14
1

You aren't comparing two strings.

You are comparing a string with the innerText property of an identical string

myArray[i] === x
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 1
    myArrey[i] is an HTMLParagraphElement – petko_stankoski Oct 01 '12 at 12:52
  • It doesn't return false when I construct [an example](http://jsfiddle.net/Bkrnu/) based on the code you provided. – Quentin Oct 01 '12 at 12:53
  • If it is an HTMLParagraphElement, then its `innerText` property does not march the string you are testing against. Most likely there are differences in white space. – Quentin Oct 01 '12 at 12:54
  • If I [change my example to use a paragraph](http://jsfiddle.net/Bkrnu/1/) then it stil works (only tested in Chrome). – Quentin Oct 01 '12 at 12:56
  • But I don't define myArray on same way as you. – petko_stankoski Oct 01 '12 at 13:20
  • @Srcee — You added a bunch of information about how you were constructing it (not that 'it' is an array, it looks like a jQuery object) after I wrote my answer. Your current question doesn't provide all the information needed to reproduce the problem anyway. – Quentin Oct 01 '12 at 13:24
0

It looks like you should compare myArray[i] rather than myArray[i].innerText, seeing as that is what you assign to. myArray[i].innerText will always yield undefined, if myArray[i] is a string (unless you're doing some weird prototyping).

David Hedlund
  • 128,221
  • 31
  • 203
  • 222