2

As part of a jQuery plugin, a strange object format is pulled using $.get(), meaning that DOM traversal isn't possible in any of the post-function hooks:

$.get($href)
.done(function(){
  $linkClicked.addClass('active')
})
.fail(function(){
  $.get(settings.errorUrl, function(){
    $('.main-navigation .active').removeClass('active')
  })

})
.always(function(data){

  // The below line does not work correctly
  $(settings.target).hide().html( $(data).children(settings.target).html() ).fadeIn('fast')

})

If somebody could lend a hand, that'd be great. Many thanks.

3 Answers3

2

I guess you can do like this:

$.get('ajax/test.html', function(data) {
 var container = $('<div />').html(data);
 var contentYouNeed =  container.find('#ajaxID').html();
});

As you can see on the document here ( http://api.jquery.com/jQuery.get/), The returned data by jQuery.get() is a PlainObject or a String.

data
Type: PlainObject or String
A plain object or string that is sent to the server with the request.

To retrieve the contents from this object, you put this data in a <div> object like this.

 var container = $('<div />').html(data);

And you can use find() to get the content.

var contentYouNeed =  container.find('#ajaxID').html();
naota
  • 4,695
  • 1
  • 18
  • 21
1

No no no no. You don't use regex for parsing HTML please read this.

Also, to solve you problem use jQuery selectors: $("#yourID").html()

Community
  • 1
  • 1
Stasel
  • 1,298
  • 1
  • 13
  • 26
0

So the response is always one div element? In that case it's just a matter of grabbing the contents of it. No matter what the ID and attributes are.

$.get('ajax/call.php', function(data) {
    var contents = $(data).html();
});
Eric
  • 18,532
  • 2
  • 34
  • 39