views:

365

answers:

1

I am trying to get the text inside a specific div in a server response. I used Firebug to see what the response was, and I can see my element in the returned code, but for some reason I can get jQuery to capture it. Here is what I am using:

var response = $('<div />').html(serverData);
$('#uploadedFiles').html($(response).find("#created").text());
alert($(response).find("#created").text());

Trying that just returns nothing, not text or anything. Am I doing this correctly?

Note: The server response is not from a jQuery ajax function, rather from the jQuery SWFUpload plugin, would this matter though?

+1  A: 

When are you running the code? If you run it before the uploadedFile element is created, the code will not find it.

I tested this, and it works just fine, it alerts "asdf" and then replaces "test" with "asdf" in the div element:

<script type="text/javascript">

$(function(){
    var response = $('<div />').html('<div id="created">asdf</div>');
    alert(response.find("#created").text());
    $('#uploadedFiles').html(response.find("#created").text());
});

</script>

<div id="uploadedFiles">test</div>

Note that response is alread a jQuery object, so $(response) is redundant.

Guffa
I am using this in the uploadSuccess function which has the serverData param to use.Your example is just finding some location ID, would it be different grabbing it from the serverData param?
Nic Hubbard
Ah, I got it working. I did some more testing and having an entire html document was screwing it up. I fixed my script to just return the code in the <form> tag and it works perfectly.Thanks.
Nic Hubbard
@Nic: Yes, if you pass any complex html code into the $ method, it will create the element by assigning it to innerHTML of a div element, so you can't use a complete html document.
Guffa