for given tag I need to wrap it with quite a few elements.
So that user can put
<img src="img.gif" >
but outcome would be
<div class="total">
<div class="cover">
<div class="crop">
<img src="img.gif" alt>
</div>
<div class="hover">
<div class="peel">
<div class="links">
<a href="#">FB</a>
</div>
</div>
</div>
</div>
</div>
So i decided to make it into jQuery plugin. And here is piece of it
(function($) {
$.fn.peel = function(options) {
var defaults = {};
var settings = $.extend({},defaults, options);
return this.each(function() {
$(this).load(function() {
var image = $(this);
var total = $('<div />', {'class' : 'total'});
var cover = $('<div />', {'class' : 'cover'});
var crop = $('<div />', {'class': 'crop'});
var hover = $('<div />', {'class' : 'hover'});
var peel = $('<div />', {'class' : 'peel'});
var links = $('<div />', {'class' : 'links'});
var a = $('<a>', {'href' : '#', 'text' : 'FB'});
//Chaining begins here
image.wrap(crop);
crop.after(hover);
});
});
};
})(jQuery);
At first i make variables with divs i will use. But i am having hard time making it all together this time..
I left 2 lines which doesnt want to work with chaining. At first i am wrapping image with crop, and then i am adding hover div after crop div. But… 2nd line doesn’t work. I tried in numerous ways. No luck so far. Seems like it is impossible to add something that is added with “wrap”. Maybe I am doing wrong, so maybe someone of you could help ? 🙂
Thanks.
you can achieve that in this way:
@dystroy’s answer is not totally correct: you can insert thing after a node in memory
you’ll find all the 3 nodes were appended to DOM
the key point is
$.wrap. when you wrap something, not like other operation, jQuery will create a new copy, at this time, the wrapper in the dom is not the same reference with the one created in memory any more.take the first line as example:
the last but not least, in actually, I don’t like this way to extend the dom node. it made the code hard to write and maintain.
here’s my solution:
jsFiddle Demo: http://jsfiddle.net/vF4QH/1/