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?
jQuery

Avatar of undefined
Last Comment
leakim971
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
jQuery
jQuery

jQuery (Core) is a cross-browser JavaScript library that provides abstractions for common client-side tasks such as Document Object Model (DOM) traversal, DOM manipulation, event handling, animation and Ajax. jQuery also provides a platform to create plugins that extend jQuery's capabilities beyond those already provided by the library.

19K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo