troubleshooting Question

jQuery wrap-around and append

Avatar of tonelm54
tonelm54 asked on
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>");

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>

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


Open in new window

Any ideas?
Join our community to see this answer!
Unlock 1 Answer and 1 Comment.
Start Free Trial
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