Déjà Bond
Déjà Bond

Reputation: 301

jQuery: Use insertAfter method to create p tag inside div tag

I want to create a div tag with p tag inside it by insertAfter method

So now My code is

$('<p></p>', {
  'width': '110',
  'height': '110',
}).insertAfter($(this));

The output now like this

<p style="width:110px;height:110px;"></p>

How to make the output to be like this without repeating insertAfter method

<div id="wrapP"><p style="width:110px;height:110px;"></p></div>

Thank You

Upvotes: 0

Views: 133

Answers (2)

Adil Shaikh
Adil Shaikh

Reputation: 44740

$('<p></p>', {
  'width': '110',
  'height': '110',
}).insertAfter($(this)).wrap('<div id="wrapP"></div>');

Demo --> http://jsfiddle.net/PCQrP/3/

Upvotes: 2

Nick Tomlin
Nick Tomlin

Reputation: 29251

My approach requires creating the parent element first, and then appending the child:

$('<div id="wrapP" />').append($('<p></p>', {
  'width': '110',
  'height': '110',
})).insertAfter('body');

http://jsfiddle.net/dvz7M/

Upvotes: 1

Related Questions