Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting only a part of a file

Tags:

jquery

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.

like image 273
Mark Avatar asked Jan 02 '26 06:01

Mark


2 Answers

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.

like image 71
Paolo Bergantino Avatar answered Jan 04 '26 08:01

Paolo Bergantino


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

like image 32
cgp Avatar answered Jan 04 '26 09:01

cgp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!