Mefhisto1
Mefhisto1

Reputation: 2228

selecting inner html dom element with Jquery

how can I select the input element in something like this:

<div id="myDiv>
<input id="inp" />
<p>Hello</p>

</div>

When I select myDiv, I get that innerHtml is a string of input and paragraph elements. Is it possible to select just my input element ?

innerHTML: "    <input id="myDiv"/> <p>Hello</p> "

Upvotes: 0

Views: 56

Answers (2)

joews
joews

Reputation: 30340

The easiest way is to select the input directly:

$('#myDiv > input');

If, for some reason, you need go through the selection containing myDiv, use find to select a descendent of the current selection:

var selection = $('#myDiv');
selection.find('input');

If you need to access the DOM element rather than the jQuery selection, use subscript syntax or get:

$('#myDiv > input')[0];
$('#myDiv > input').get(0);

Upvotes: 1

Balachandran
Balachandran

Reputation: 9637

Try to use the .outerHTML property,

$('#inp')[0].outerHTML //<input id="myDiv"/>

DEMO

Upvotes: 2

Related Questions