2

I'm using jQuery .load() to get info out of a database. Before loading the info into a div i would like to check the content with a if statement.

For example: (the ajax will return 1)

$('div.status').load("../files/ajax-calls.php?type=validatePerformer&performerId=265486");

if($('div.status').text() == 1){
  alert("ajax input checked, value = 1");
}else{
  return false;
}

i don't know if it's possible to check incoming ajax variables this way because jQuery write these directly into the DOM.

The solution i'm seeking would look like this (i know this isn't correct coding, just to give an idea)

   var ajaxResult = $('div.status').load("../files/ajax-calls.php?type=validatePerformer&performerId=265486");

    if($(ajaxResult) == 1){
      $('div.status').text(ajaxResult);
    }else{
      return false;
    }

Thanks in advance!

1

1 Answer 1

1

You can pass a callback to the .load function...

$('div.status')
    .load("../files/ajax-calls.php?type=validatePerformer&performerId=265486",
          function() { alert("complete"); })

...but it won't prevent jQuery from inserting the new content, since this is done before the callback occurs.


To manually insert based on a condition, you should probably use $.get instead.

$.get("../files/ajax-calls.php?type=validatePerformer&performerId=265486",
      function(ajaxResult) {
          if(ajaxResult == 1)
             $('div.status').text(ajaxResult);
      });
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.