A user can upload a picture on my site at which point jQuery inserts it into the page with append(). After appending I need to able to determine if there is something there or not, but jQuery’s “is:empty” check returns true despite content having been appended. If I reload the page, the “is:empty” check will return false. It seems that jQuery is failing to update the DOM when appending and is then checking against its non-updated model when running “is:empty”. Does anybody know if there is a way around this?
EDIT:
Added from the comments –
if ($('#issueimage1'+issueid).is(':empty')) {
$('#issueimage1'+issueid).append(responseText);
}
elseif ($('#issueimage2'+issueid).is(':empty')) {
$('#issueimage2'+issueid).append(responseText);
}
else {
$('#issueimage3'+issueid).append(responseText);
}
The idea is that the user can add up to three pictures. I have a dedicated element for each picture. It checks each element sequentially to see where to insert. This works fine initially (if, on page load, there are two pictures, it will insert into the third slot). When adding two images without refreshing the page, it fails
When you say “
is:empty” do you mean?
It’s working ok for me, as demonstrated in this Working Demo
EDIT:
The problem with using
:emptyis that it also assesses whether an element contains text nodes as well as element nodes, and if it contains text nodes, is not considered empty. I don’t think that this is the behaviour you want so we can use.lengthon a wrapped set of the child elements of a<td>to ascertain whether it has any child element nodes; if the length is 0, then it is considered empty for this purpose (checking.lengthis the same as using.size()which calls.length, except checking.lengthdirectly will be slightly faster).So, let’s say we have a
<tr>referenced bythisHere’s a Working Demo to show you an example. Add /edit to the URL to see the code and play with it.
jQuery Code (I wrote this in a hurry so could probably be tidied up somewhat)
HTML