i’m creating a img when i click in a input, then i get the html or anyelse from the created img.
but i dont know why this is not working!
always return null
my js:
$("#click").click(function(){
var $imgDone = $('<img/>').attr('src', 'someImage/insomewhere.jpg');
$(this).after($imgDone);
setTimeout(function(){
alert($(this).next().html());
}, 1000);
});
i mande a exp.: Demo
thisis pointing at the wrong place insetInterval.The
thisyou have in the outer scope isn’t the same as thethisthat you get inside the callback, which will usually be thewindowobject, since:is actually
To solve the problem, take a copy of
thisin the outer scope:For efficiency, as @T.J. Crowder suggests, you could actually use the jQuery constructor to take that copy, and save yourself a few calls to jQuery:
The other problem is that
.html()shows the inner contents of tags, not the tags themselves, and yourimgtag has no inner contents.There doesn’t appear to be any builtin jQuery method that returns the actual whole HTML of an element. To do that you’d need to put your element into something else (e.g. a
<div>) and then take the.html()of that.Here’s a plugin I just made that does this, inspired by something I found via Google:
Demo of it in use on your problem at http://jsfiddle.net/alnitak/qaSmS/