troubleshooting Question

jQuery wrap-around and append

Avatar of tonelm54
tonelm54 asked on
jQuery
1 Comment1 Solution22 ViewsLast Modified:
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>");

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

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>

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();

Any ideas?
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 1 Comment.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros