0

I thought this would be easier to find :)

$.get('/page.html', function(data) {
    //get only contents of class="contents"
});

I can't seem to find out how to do that.

Ian
  • 50,146
  • 13
  • 101
  • 111
Wesley
  • 5,381
  • 9
  • 42
  • 65
  • 1
    can you please elaborate what do you exactly mean by "get only contents of class='contents'" – Ali Shah Ahmed Apr 06 '13 at 03:59
  • Sure. The page in question has a lot of generated code I want to ignore. I only want to get back a specific section of the page I'm loading. – Wesley Apr 06 '13 at 04:00
  • @dinjas Logging it out just shows me the entire page. How do I convert the returned data into an object that allows me to select a specific element? – Wesley Apr 06 '13 at 04:00

3 Answers3

2

Just use .find():

$(data).find(".contents")

Here's the final code you could use:

$.get('/page.html', function(data) {
    var contents = $(data).find(".contents");
});

Then depending on what type of element the .contents is, you could use contents.html() or contents.text() (most elements), or contents.val() (input element).

Technically, another idea is to use .filter().

This all depends on the structure of your HTML in data. If your structure is like this:

<div id="main">
    blah blah
    <div class="contents"></div>
</div>

Then the result of $(data) will be [#main] and you need to use find. If your structure is like:

<div id="main"><div>
<div class="contents"></div>
<div class="another"></div>

Then the result of $(data) will be [#main, .contents, .another] and you need to use filter.

Of course, combining them, it could be:

$.get('/page.html', function(data) {
    var all = $(data), contents;
    contents = all.filter(".contents");
    if (!contents.length) {
        contents = all.find(".contents");
    }
    if (contents.length) {
        // It/they was/were found at some point
    }
});
Ian
  • 50,146
  • 13
  • 101
  • 111
  • Surprisingly, I did try that. Didn't work. I just get back a jQuery object, but cannot access the .text() or .html() – Wesley Apr 06 '13 at 04:03
  • @Wesley What is the result of `$(data).find(".contents").length` (like if you `alert` it) – Ian Apr 06 '13 at 04:04
  • @Wesley And what is the HTML structure of `data` anyways? – Ian Apr 06 '13 at 04:05
  • Length is 0. Based on the fast that fletch's answer works, there seems to be some dependency on casting the object before I run .find() or .filter()? – Wesley Apr 06 '13 at 04:08
  • @Wesley No, nothing special. Both put `data` in a jQuery object - the "required" part. But it all depends on what your HTML structure that's returned from `/page.html`. Sometimes there's one big element that contains all the elements (and the one you're looking for). Sometimes, there's several elements next to each other (also with elements inside) but one of those are the ones you're looking for. `find` and `filter` do different things. `find` looks at the descendents of the selected elements. `filter` looks at the selected elements – Ian Apr 06 '13 at 04:10
  • 1
    If you can't be certain of your returned HTML structure, why not add a conditional in there that checks the length following the use of `.find()`...if `0`, then give `.filter()` a shot? – fletch Apr 06 '13 at 04:27
  • 1
    @fletch Very good point. Hadn't thought of that. Although I'd start with `filter` and then move to `find`. Either way, I added to my answer, so thanks for suggesting that – Ian Apr 06 '13 at 04:32
  • Interesting, fun discussion! It seems that the post fletch refers to mentions that starting with .find() always returns an empty jquery object. – Wesley Apr 06 '13 at 04:32
  • @Wesley Well to me, starting with `filter` makes more sense because it filters/searches the parent elements only, which there should usually be less of, while `find` searches all descendents of all parents, which I would think there would be more of. Anyways, glad you're enjoying the discussion :) – Ian Apr 06 '13 at 04:42
2

I would do something like this...

$.get('/page.html', function(data){
   var $page = $(data)
   var $contents = $page.filter('.contents'); /* this will get a reference to the .contents */
   /* do more stuff here 
      ....  
   */
});

Have a look at the accepted answer to this SO: Get HTML of div element returned by .get with jQuery

If the structure of the returned content is uncertain (to where sometimes you would need to use .find() due to .contents being a descendent, you could always perform a check and run .filter() or vice versa.

Example:

$contents = $page.filter('.contents');
if($contents.length == 0) {
   $contents = $page.find('.contents');
}

Just a thought based on the comments posted in other answers.

Community
  • 1
  • 1
fletch
  • 1,631
  • 1
  • 11
  • 12
  • Yeah that's a good point, it definitely depends on the structure of the HTML in `data` – Ian Apr 06 '13 at 04:05
  • I had `.find` first, but then I came across the other question with a quick Google search. – fletch Apr 06 '13 at 04:07
2

If you create a jQuery object from the returned data and the wrapper element in the jQuery set is .contents element then find fails to find the target element and if the contents element is a descendant element then filter method fails to select that element. You can create a dummy element, populate it with returned data and use find method.

$.get('/page.html', function(data) {
    var element = $('<div/>').html(data).find('.contents');
});
Ram
  • 143,282
  • 16
  • 168
  • 197