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
  • 259,174
  • 73
  • 529
  • 557
Madrugada
  • 1,181
  • 8
  • 22
  • 44

3 Answers3

4

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

Engineer
  • 45,891
  • 11
  • 86
  • 90
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
  • 45,503
  • 23
  • 111
  • 144
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
  • 244
  • 4
  • 16