Link to home
Start Free TrialLog in
Avatar of tonelm54
tonelm54

asked on

jQuery wrap-around and append

Im trying to wrap an element in a div, but also add an element in, what Ive tried to do it:-
$("#txtLabel1").wrapAll("<div class='divBox'></div>").prepend("<label for='txtLabel1'>ergerg</label>");

Open in new window


The element Im trying to wrap around:-
<input type="text" id="txtLabel1" />

Open in new window


And what Im trying to do it make it so its like
<div class='divBox'>
<input type="text" id="txtLabel1" />
<label for='txtLabel1'>Label Text</label>
</div>

Open in new window


I need to do it all in one line, as I want to put it in a plugin, so it will wrap around and append for each element.

My final solution needs to be in a plugin, so something like:-
$.fn.labelSize = function(options ) {
		var settings = $.extend({
			// These are the defaults.
		}, options );
		
    return this.each(function() {
				var thisElement = $(this).clone();
				var thisWatermark = $(this).attr("placeholder");
				var thisID = $(this).attr("id");
        
				$(this).replaceWith("<div class='tc-LabelSize'>" + thisElement + "<label for='" + thisID +"'>'" + thisWatermark +"'</label></div>" );
			
    });
};

$(".labelSize").labelSize();

Open in new window


Any ideas?
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial