0
   <a id="jobDescription" class="is_html" tabindex="-1" href="/es/backoffice/jobs?job_id=1"><p>Este puesto implica una gran capacidad de organización: - Recibir pedidos - Bla bla.... Y muchas cosas m</p><p>&nbsp;</p><p>pruebassssssssssssssssss</p><p>&nbsp;</p><p>adsfasdfsafad adfasdf asdfas s gfasdpepeluis &nbsp;don chencho&nbsp;</p></a>

   <input type="hidden" value="<p>Este puesto implica una gran capacidad de organización: - Recibir pedidos - Bla bla.... Y muchas cosas m</p><p>&nbsp;</p><p>pruebassssssssssssssssss</p><p>&nbsp;</p><p>adsfasdfsafad adfasdf asdfas s gfasdpepeluis &nbsp;don chencho&nbsp;</p>">

I have got those two HTML tags. I need to compare the values and text they have, but the problem is that whatever I do, I'm getting a false value even though they are actually the same. Here my jQuery code:

oldValue = $(elem).find("input[type='hidden']").val();
newValue = $(elem).children()[0].innerHTML.replace(/&nbsp;/g,' ');;

As you can see, the hidden element is keeping oldValue, and the a link is keeping newValue. Using Google Chrome's developer tool, I can use the console to print out the values right before comparing them, getting this result:

newValue
"<p>Este puesto implica una gran capacidad de organización: - Recibir pedidos - Bla bla.... Y muchas cosas m</p><p> </p><p>pruebassssssssssssssssss</p><p> </p><p>adsfasdfsafad adfasdf asdfas s gfasdpepeluis  don chencho </p>"

oldValue
"<p>Este puesto implica una gran capacidad de organización: - Recibir pedidos - Bla bla.... Y muchas cosas m</p><p> </p><p>pruebassssssssssssssssss</p><p> </p><p>adsfasdfsafad adfasdf asdfas s gfasdpepeluis  don chencho </p>"

newValue == oldValue
false

What could possibly be wrong here??

rmartrenado
  • 1,516
  • 1
  • 18
  • 42
  • 1
    Can you reproduce it on http://jsfiddle.net/ – Adil Shaikh Dec 10 '14 at 11:11
  • you haven't replaced the non-breaking spaces in the old value – Pete Dec 10 '14 at 11:14
  • 1
    Your issue is with spaces, I found a similar answer here : http://stackoverflow.com/questions/22036576/why-does-the-javascript-string-whitespace-character-nbsp-not-match – kasperoo Dec 10 '14 at 11:22
  • I have tried replacing spaces in oldValue, and still getting the same problem. If I can see in the console that thery are exactly the same, newValue == oldValue should return TRUE right?? – rmartrenado Dec 10 '14 at 11:31

2 Answers2

0

I see you have replaced &nbsp in newvalue but not in oldvalue maybe that could mean something...

can you post complete code to make a clearer idea of what are you doing?

EDIT: If it can help: What is the correct way to check for string equality in JavaScript?

Community
  • 1
  • 1
fredmaggiowski
  • 2,232
  • 3
  • 25
  • 44
0

apparently, using replace(/&nbsp;/g,' ') is not enough for compare a string in a variable and a string from an element.

following this example, I managed to create a fiddle which resolve your problem:

link to fiddle

Community
  • 1
  • 1
Florian
  • 855
  • 7
  • 12