Madrugada
Madrugada

Reputation: 1289

Create a DOM element with jQuery

I created a input element with jQuery like that:

var element = $('<input />', {
  name: name1,
  id: id1,
  type: 'text'
});

And I want to return the DOM element associated with it. However, element is not a DOM (yet). How can I transform it to DOM element?

Upvotes: 4

Views: 399

Answers (3)

Alex Turpin
Alex Turpin

Reputation: 47776

jQuery returns an array-like object of DOM elements. Using the array indexer will always allow you to get a specific element, like so:

var domElement = element[0];

Alternatively, jQuery provides a function just for that, .get:

var domElement = element.get(0);

Upvotes: 4

Kornel
Kornel

Reputation: 263

you can check this solutions to use maybe a DOM parser. Converting HTML string into DOM elements?

or just use some native javascript functions. Creating a new DOM element from an HTML string using built-in DOM methods or prototype

Upvotes: 1

Engineer
Engineer

Reputation: 48813

You could use element[0] or element.get(0).

Upvotes: 4

Related Questions