Link to home
Start Free TrialLog in
Avatar of Jamie
JamieFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Cannot process .load result correctly to change and existing set variable

Hello!

I'm trying to write some code that verifies and email address, returning Y if valid and N if not - this bit works fine, with the ValidEmail div id having the right result returned - i.e. either Y or N. However, I cannot get the valid_flaf indicator set correctly after the load event?

The alert of valid_flag always returns 0, irrespective if ValidEmail').html()  is Y or N

-------------------------------

valid_flag=9;

$('#ValidEmail').load('/Validate_EMAIL.asp?VerifyEmail=' + document.TestForm.AdminEmail.value,function(){

      if ($('#ValidEmail').html() == "Y") {
            valid_flag=1;
      } else {
            valid_flag=0;      }});

alert(valid_flag);

-------------------------------


Kind Regards

JamWales
Avatar of Tom Beck
Tom Beck
Flag of United States of America image

I would say because the html for the ValidEmail div never equals "Y".

Try:

if ($('#ValidEmail').text() == "Y") {

. . .
Avatar of Jamie

ASKER

Hi tommyBoy,

Thanks for the above.

It seems to work, in that the valid_flag now seems to be set correctly - although the alert(valid_flag); still shows 0, even when it appears to be set to 1 - but my calculation further down now works...

... however, if i remove the alert(valid_flag); statement, the valid_flag is not set correctly?!

Kind Regards

JamWales
You have code in addition to what was posted here and that is affecting the outcome. I say that because this simple test works.
<!DOCTYPE html>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
  $(document).ready(function(){
      var valid_flag=9;
  
      if ($('#ValidEmail').text() == "Y") {
            valid_flag=1;
      } else {
            valid_flag=0;      }//}); 

      alert(valid_flag);
  });
</script>
</head>
<body>
    <div id="ValidEmail">Y</div>
</body>
</html>

Open in new window

Avatar of Jamie

ASKER

Hi tommyBoy,

Yes, as you say, your script works fine - but the problem is as originally described, with the .load command. I'm not even sure what I'm trying to do is possible?

Basically, the response from the Validate_EMAIL.asp script is placed into the <div id="ValidEmail"></div> - which is then used to determine the value of valid_flag by function().

Kind Regards

JamWales
Avatar of Jamie

ASKER

Hi tommyBoy,

I've found the cause of my problem, but not the solution!

I've amended your script below to show the issue. In my script, the variable valid_flag is set outside the .load function, so the change is not being reflected - hence why the alert(valid_flag); always returns 0 - instead of 1 for Y and 0 for N.

The below alert box will incorrectly display 9 instead of 1, as Y in ValidEmail in div tag.

Any ideas on how to resolve this?

Kind Regards

JamWales

<!DOCTYPE html>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">

   var valid_flag=9;

  $(document).ready(function(){
   
 
      if ($('#ValidEmail').text() == "Y") {
            valid_flag=1;
      } else {
            valid_flag=0;      }//});

     
  });
 
  alert(valid_flag);
 
</script>
</head>
<body>
    <div id="ValidEmail">Y</div>
</body>
Any script in the head section that's not in a function is executed before the window.onload event or in this case the jquery equivalent, $(document).ready().

So...the alert is executing before the code inside the ready() function. Variable valid_flag equals 9 at that point.
Avatar of Jamie

ASKER

Hi tommyBoy,

Thanks for your reply, I understand what you are saying, but my code below is placed into an existing function, so is not called until the function is called by the user pressing a text link on the webpage - this calls; javascript:total_cost()

From what you say and from what happens, it appears the .load function is being run local to itself and it's results not available to the function it resides it.

So not sure if it is the construct of my .load is still the problem?

Kind Regards

JamWales


valid_flag=9;

$('#ValidEmail').load('/Validate_EMAIL.asp?VerifyEmail=' + document.TestForm.AdminEmail.value,function(){

      if ($('#ValidEmail').html() == "Y") {
            valid_flag=1;
      } else {
            valid_flag=0;      }});

alert(valid_flag);
ASKER CERTIFIED SOLUTION
Avatar of Tom Beck
Tom Beck
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jamie

ASKER

Explained the solution - and me realising what I'm trying to do in my script isn't possible.