4

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?

Sampson
  • 265,109
  • 74
  • 539
  • 565
Madrugada
  • 1,261
  • 8
  • 24
  • 44

3 Answers3

4

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

Engineer
  • 47,849
  • 12
  • 88
  • 91
4

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);
Alex Turpin
  • 46,743
  • 23
  • 113
  • 145
1

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

Community
  • 1
  • 1
Kornel
  • 263
  • 4
  • 16