Wednesday, 7 August 2013

jQuery .html() anonymous function return oldHtml

jQuery .html() anonymous function return oldHtml

Fiddle: http://jsfiddle.net/bplumb/ZnMF5/1/
This is obviously some lack of understanding on my part, but I am trying
to run an anonymous function with .html() to do some custom code and then
return the old html string value to a variable. It isn't returning the
html as I would expect but is returning a jQuery object based on the
selector I used.
var oldHtml = $('#test').html(function(index, oldHtml){
//some custom code here
return oldHtml;
});
console.log(oldHtml);
I thought I could return the html this way as it works in a normal
function call.
var someOtherHtml = getOldHtml();
console.log(someOtherHtml);
function getOldHtml(){
return $('#test').html();
}
What am I not understanding about jQuery when it comes to this?

No comments:

Post a Comment