tags:

views:

80

answers:

2

I have some very simple sample code like this:

$.ajax({
  url: 'demo2.htm',
  success: function(loadeddata){
    $("#loaded_data").html(loadeddata);
    alert('success');
  },
  error: function(){
    alert('failure');
  }
});

Loaded data currently returns everything. What I need to is to get only a specific div and make it the html of #loaded_data.

How do I do that?

Thanks!

Edit:

While trying to use .load()...

Here's what I wrote into the comment.

Thanks, your updated example is great. However I'm not sure what's wrong with mine.

This: works

$("#loaded_data").load("demo2.htm #mydiv", function(text, status, request) {
    if(status == 'success') {
        alert('success');
    } else {
        alert('error');
    }
});

This:

$("#loaded_data").load("demo5.htm #mydiv", function(text, status, request) {
    if(status == 'success') {
        alert('success');
    } else {
        alert('error');
    }
});

Does not. It just hangs. There is no demo5.htm But no error is returned.

Thanks a lot again for all of your help.

+5  A: 

You could use .load() for this:

$("#loaded_data").load("demo2.htm #mydiv", function(text, status, request) {
    if(status == 'success') {
        alert('success');
    } else {
        alert('error');
    }
});

If this isn't working for you, you're doing something wrong and we'd need to see some more code.

Here is a sample of getting a single div from this page to this page using the code above.

Paolo Bergantino
Sorry, is there some way to do the same with .ajax? I've tried .load, but was unable to get success/error to work correctly:http://stackoverflow.com/questions/936357/checking-jquery-ajax-load-success
Mark
You were first here, and also went through the trouble of the example. Nice.
altCognito
Thanks, your updated example is great. However I'm not sure what's wrong with mine.This: works$("#loaded_data").load("demo2.htm #mydiv", function(text, status, request) { if(status == 'success') { alert('success'); } else { alert('error'); }});This: $("#loaded_data").load("demo5.htm #mydiv", function(text, status, request) { if(status == 'success') { alert('success'); } else { alert('error'); }});Does not. It just hangs. There is no demo5.htm
Mark
Mark: Here is an example of it linking to a URL that doesn't exist: http://jsbin.com/ihafo - it gives me error just fine. Perhaps it's something to do with your webserver?
Paolo Bergantino
Could be, I'm not on a server though, just trying to edit in a folder on my desktop. Would that make a difference? But well, so long as it works on a server I'll be happy then. Thank you very much, and thanks for your patience.
Mark
OK, some further thoughts. But $.ajax correctly returns the error even though it's also not on the server, so that's why I'm inclined to like it more.
Mark
http://jsbin.com/inadu <- though this is still no good.
Mark
+1  A: 

I've updated my other example, but I'll clone it into this question which is probably questionable (no pun intended)... but alas, we've got two questions for the same thing...

Check the textStatus of the response.

$("#links").load("/Main_Page #jq-p-Getting-Started li", 
  function (responseText, textStatus, XMLHttpRequest) {
    if (textStatus == "success") {
         // all good!
    }
    if (textStatus == "error") {
         // oh noes!
    }
  }

In determination to figure out the issue:

An example of success:

http://jsbin.com/ageju

An example of fail:

http://jsbin.com/arite

altCognito
Ah you slick one. I'll upvote you here too because I'm nice. ;)
Paolo Bergantino
Thank you too!!
Mark
Though it would have been nice to learn how to get just a part of the return. Oh well :P some other time I guess.I selected you as the correct answer on the other question. Thanks!
Mark