tags:

views:

30

answers:

1

i use the jquery get to fetch some plain html content form a file. In the file if have several div elements and depending on the page 1 of the has to be shown.

I have a workaround for the moment where i just load all the data en than hide the div's i don't need, but i looking for a better way.

the code for the moment

      $.get(urlInfo + "xxxxxxx.html", function(data){
   $("#contentArea").append(data );
   $("#contentArea > #popupInfo").hide();
  })

so is there a way i can just directly select the div i need from the 'data' variable.

tnx

+1  A: 

You could always use

var myDiv = $(data).find('#theDivIWant');
$('#contentArea').append(data);
David Hedlund
doesn't work.if i do a alert of 'mydiv' i get [object][Object]
Dazz
and it is an object, so that's quite alright. a jquery-object. you need to modify the `find` selector to fit your needs. consider this example: `var myDiv = $('<div><div id="id1"></div><div id="id2"></div></div>').find('#id1')` Here *myDiv* will be a jQuery-array containing only *div#id1*
David Hedlund
you mean it only contains the div and not it's children?so should it look something like this?var myDiv = $(data).find("#popupInfo > *");$("#contentArea").append(myDiv);but that doesn't work.
Dazz
well yes, it will contain its children, just like a regular jquery selector, it's just that in my example, the div of interest didn't have any children. i guess you haven't revealed enough of your HTML structure for me to be able to help you here. if you want to show *everything* in `data` except for `#popupInfo` with children (if it has any), that would be `$('#contentArea').append($(data).not('#popupInfo'))`
David Hedlund
i'm sorry to say that doesn't work either, i get the error 'message: Statement on line 1: Type mismatch (usually non-object value supplied where object required)'my html structure is very simple. i have 2 div's and inside them there is some plain text, thats it.
Dazz
found the problem, my 2 div's just had to be in 1 'container' div and then the find function works like a charm. tnx
Dazz
well, consider this simple example: http://pastebay.com/72560here `getCallback` corresponds to the function you would pass to your `$.get`, and `stringFromGet` is the HTML string that's being passed to `getCallback`. out of the two DIV's in the string, I'm selecting one that is to be shown, and I append it to `contentArea`. You can paste that code into a blank html file, and it'll work. based on that, i think you should be able to adjust it to your needs
David Hedlund
alright, i'm glad you got it working
David Hedlund