1

I'm using simple jQuery to compare two variables, but every time it is yielding false result. Here is the code piece:

 var fromIM = $("#passwordFroma").val();
 loadContent(passwordValentered);
 var encrypt = document.getElementById("prtCnt").value;
 alert("ajax call " + encrypt);
 alert(encrypt == fromIM);

In the above code piece,

passwordFroma is a hidden text field.
passwordValentered is a text box to get user input.
prtCnt is a hidden field.

Also, loadContent(passwordValentered) function is an ajax call which sets the value for hidden field prtCnt. This is confirmed from the first alert. But, when I compare the values in second alert, I always get the result as false.
Please let me know where am I going wrong! I'm using jQuery 1.9.

4
  • 1
    Have you checked to see what encrypt and fromIM look like (i.e. their values) in your JavaScript console? Commented May 7, 2013 at 20:10
  • 4
    Ajax is asynchronous. Make sure you do the alert(encrypt == fromIM) call in the success function of the ajax call. Commented May 7, 2013 at 20:11
  • I agree. You should do something like `alert(fromIM + " == " + encrypt + "?"); to see if the values are what you think they are. Commented May 7, 2013 at 20:12
  • 1
    @summea yes..I've checked them and they are the same!! Commented May 7, 2013 at 20:12

1 Answer 1

3

Your generated field with id prtCnt is generated asynchronously (AJAX), therefore it's not accessible immediately after the call to loadContent(passwordValentered);

var fromIM = $("#passwordFroma").val();
// Sends AJAX
loadContent(passwordValentered);
// AJAX is not finished here
var encrypt = document.getElementById("prtCnt").value;
alert("ajax call " + encrypt);
alert(encrypt == fromIM);

You have to pass a callback to loadContent

var fromIM = $("#passwordFroma").val();
loadContent(passwordValentered, function(){
   var encrypt = document.getElementById("prtCnt").value;
   alert("ajax call " + encrypt);
   alert(encrypt == fromIM);
});

And modify your loadContent so that it calls the given callback from the success handler of $.ajax

Sign up to request clarification or add additional context in comments.

1 Comment

Ok Juan, let me try that. As I'm pretty new to Ajax and jQuery, so this all might take some time and it's already 2:00am at my place, I'll try it tomorrow and will update you soon. Thanks for the suggestion!!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.