0

I have a hidden field with value "8" and I return json data from my asp.net mvc controller where the templateId has a value of 8.

"8" is not 8. Thats fine but how can I compare then both values?

enter image description here

Elisabeth
  • 20,496
  • 52
  • 200
  • 321
  • 2
    It equals to string with 3 chars `"8"`. Quote, digit 8, quote. "but how can I compare then both values" - at first you need to find where the quotes come from – zerkms Sep 27 '12 at 21:11
  • do you compare a string with a number? – mistapink Sep 27 '12 at 21:11
  • 1
    Couldn't it be that the output is just putting quotes around it to show that it is a string? I'm sorry but I do not recognize this dev environment on the screenshot, so I do not know. – Marten Sep 27 '12 at 21:20
  • With the values you show, there is no reason that should not evaluate to true. Visual Studio does add the quotes around the output of text(), so thinking that "8" is the literal is a red herring. – John Koerner Sep 27 '12 at 21:34
  • @John Koerner: that's the only explanation why it returns false. Any *other* ideas? – zerkms Sep 27 '12 at 21:36
  • 2
    @zerkms At this point I would want to see a working version. I have verified in VS that if the literal were "8" it would show ""8"" – John Koerner Sep 27 '12 at 21:39
  • 1
    Can you debug this in a browser to ensure what's going on? Either add `console.log('Template ID: ' + response.templateId + ' Textbox: ' + $('#TemplateId').text())` right above the if statement or set a breakpoint. – Snuffleupagus Sep 27 '12 at 21:40
  • ok I have the error: I did $("#TemplateId").text() == 8 which is false, then I did $("#TemplateId").val() == 8 which is true. Sorry guys my failure. Can someone write that as solution? I stepped into this trap...: http://stackoverflow.com/questions/807867/what-is-the-difference-between-jquerys-functions-val-and-text – Elisabeth Sep 27 '12 at 21:48
  • This is really crazy. The condition is now TRUE but the code inside the if clause is never hit ??? Thats weird! – Elisabeth Sep 27 '12 at 22:02
  • Haha finally got it: instead of a direct compare I do this then my if clause is executed: var selectedTemplateId = $('#TemplateId').val(); if (response.templateId == selectedTemplateId) { // it works} Below I put all this in an answer/solution. – Elisabeth Sep 27 '12 at 22:11
  • "instead of a direct compare" --- this shouldn't change anything. Stop thinking that programming is some Voodoo magic – zerkms Sep 27 '12 at 22:23
  • voodo magic? I wrote the facts here how I have experienced it. javascript is voodoo because of browser inconsistences. – Elisabeth Sep 28 '12 at 07:33

5 Answers5

3

In JavaScript you can compare two variable with type check or without type check

eg

 8 == '8' //true /* dont check type*/
 8=== '8'   //false /* check type*/

You don't need type checking while comparing. so your code will work fine.

Since you saying that your code is not working. and value of $("#TemplateId").text() is '8'( shown in console). only possible problem is response.templateId is not equals to 8.

Anoop
  • 23,044
  • 10
  • 62
  • 76
  • @zerkms his question is : how can I compare then both values?. I gave options for comparison. – Anoop Sep 27 '12 at 21:17
  • And `==` returns false for OP. So - how strict comparison would it change? – zerkms Sep 27 '12 at 21:17
  • @zerkms Sorry, I did not consider 'strict mode'. – Anoop Sep 27 '12 at 21:18
  • but still - I don't see how this is helpful. Yes, `==` compares 2 operands, so? – zerkms Sep 27 '12 at 21:19
  • I tested it in 'strict mode'. no difference in result. – Anoop Sep 27 '12 at 21:22
  • Right. For op the `==` comparison returns `false`. Obviously `===` would return false as well. The question is - why's that. – zerkms Sep 27 '12 at 21:23
  • "You don't need type checking while comparing. so your code will work fine." -- op doesn't have type checking and code doesn't work. And that's why I said 12 mins ago that this is not an answer ;-) – zerkms Sep 27 '12 at 21:27
3

Have you tried parseInt($('#TemplateId').text())?

Sean3z
  • 3,745
  • 6
  • 26
  • 33
  • @zerkms Since response.templateId is an integer, it would cast the text from #TemplateId as an interger - allowing him to compare both values. – Sean3z Sep 27 '12 at 21:39
  • even if it's not an integer - you can compare them without any issues. – zerkms Sep 27 '12 at 21:40
2

You could apply parseInt() to the string returned from text().

Marten
  • 1,336
  • 10
  • 16
1

You can always parse "8" to an Integer with "parseInt(str, 10)"

0

Instead of writing:

if( response.templateId == $('#TemplateId').val())
{
    // never hit here
}

I did this and it worked:

var selectedTemplateId = $('#TemplateId').val();
if (response.templateId == selectedTemplateId)
{
   // it works
}

I also did a general test: 8 is the same as "8":

if( 8 == "8")
{
    // always hit here
}
Elisabeth
  • 20,496
  • 52
  • 200
  • 321