[jQuery] What am I doing wrong here -- htmlTo?
I am trying to do the following:
1) create an IMG
2) assign an SRC to the IMG
3) wrap the IMG in <br/>'s
4) insert the resultant HTML into #foo
I have been hammering at this for awhile now, and am having problems; I've tried using wrap and some of the other content-manipulation methods, but I still can't get the implementation down to something short and sweet (referencing jQuery only once). This is the best I've got so far:
var html = jQuery( '<br/><img/><br/>' )
.find( 'img' ).attr( 'src', 'image.gif' ).end();
jQuery( '#foo' ).html( html );
I realize that I could use the literal expression in place of the "html" variable, but that's no better IMO.
What I originally started out doing, was something like this:
jQuery( '<img/>' ).attr( 'src', 'image.gif' ).wrap( '<br/><br/>' ).replaceAll( '#foo' );
But, replaceAll removes the node (I only want to change its contents).
What I've noticed, is that the jQuery API provides alternative forms of 3 methods (that I don't see used very often): append, prepend, and replace. These 3 support both of the followng forms, and I believe they are 100% interchangeable:
jQuery( selector ).method( html );
jQuery( html ).methodTo( selector );
However, .html(), which is arguably more popular, only has the 1st form, but not the second. Is there an alternative form for html() that I'm missing, or is this a hole in the API that apparently isn't wanted?
Thanks in advance!