2

I've seen many guides online for create a new element in jQuery such as the following code

var input = $('<input>', {
    id: 'FormUsername',
    name: 'username',
    type: 'text',
    value: 'Enter your username...',
    focusin: function() {
        $(this).val('');
    }
}).appendTo(someElement);

This creates one input element and appends it. I want to create a div element and add this input element to it and then append it. How would i go about doing that?

Thanks, Alex

1
  • Append and add is same thing in above context. Your question does not make sense. Commented Mar 6, 2012 at 16:53

6 Answers 6

5

You can use .wrap() to wrap your element into another one before appending it:

var input = $('<input>', {
    id: 'FormUsername',
    name: 'username',
    type: 'text',
    value: 'Enter your username...',
    focusin: function() {
        $(this).val('');
    }
}).wrap('<div/>').parent().appendTo(someElement);

Note: you'd have to call .parent() because .wrap() does not return the wrapping element

DEMO


You could also do it in several steps if you need to add attributes to the wrapping div, syntax is similar:

var input = $('<input>', { ... });

// create a div element with an ID=wrapper
$('<div/>', { id: 'wrapper' })
    // append the input to it
    .append(input)
    // append the div to "someElement"
    .appendTo(someElement);

DEMO

Sign up to request clarification or add additional context in comments.

Comments

1

Same syntax, really:

var input = $('<input>', {
    id: 'FormUsername',
    name: 'username',
    type: 'text',
    value: 'Enter your username...',
    focusin: function() {
        $(this).val('');
    }
});

var div = $('<div>', {
    id: 'SomeWrapperDiv'
}).append(input);

div.appendTo(someElement);

4 Comments

Verbose... why not use wrap?
@gdoron: See comment on ShankarSangoli's answer by the OP below.
@gdoron: I'm confused; why is it a problem that there are requirements to have an id attribute?
It's o.k. but he just did it with the input, do just do the same with the div, why are you asking??? (I blame him, not you...)
0

This maybe help you:

http://jsfiddle.net/Eek7N/1/

var div = $("<div>");

var input = $('<input>', {
id: 'FormUsername',
name: 'username',
type: 'text',
value: 'Enter your username...',
focusin: function() {
    $(this).val('');
}
});

div.html("I'm the div");

div.append(input);

$("body").append(div);​

Comments

0

And another way:

$('#someElement').append($('<span></span>').append(input));

http://jsfiddle.net/xcZjt/

Comments

0

You can try this.

  var input = $('<input>', {
    id: 'FormUsername',
    name: 'username',
    type: 'text',
    value: 'Enter your username...',
    focusin: function() {
        $(this).val('');
    }
  })
  $('<div>', {
    id: 'divId',
    class: 'className'
  })
  .append(input)
  .appendTo(someElement);

4 Comments

but i want to add an id and classes to the div also using the same way i did with the input.
wrap returns the original set of elements, not the wrappers.
@JamesMontagne - I think wrap will return the element wrapped with element you pass.
But if you call append it will only append the original elements to the dom. jsfiddle.net/xzrTx/3
0

Use the wrap function:

var input = $('<input>', {
id: 'FormUsername',
name: 'username',
type: 'text',
value: 'Enter your username...',
focusin: function() {
    $(this).val('');
}
}).wrap('<div></div>').parent().appendTo(someElement);

wrap docs:

Description: Wrap an HTML structure around each element in the set of matched elements.

2 Comments

wrap returns the original set of elements, not the wrappers.
@JamesMontagne. Right. fixed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.