How do you determine if any input within div1 is focused with jQuery?
<div id="div1">
<input type="text" id="input1">
<div id="div2">
<input type="text" id="input2">
</div>
</div>
How do you determine if any input within div1 is focused with jQuery?
<div id="div1">
<input type="text" id="input1">
<div id="div2">
<input type="text" id="input2">
</div>
</div>
if($("#div1 input:focus").length > 0){
alert('An input is selected');
}else{
alert('No inputs selected.');
}
Fiddle: http://jsfiddle.net/6r8RQ/
Short and to the point:
if ( $("#div1").find("input:focus")[0] ) {...}
Given a jQuery object that represents a set of DOM elements, the .find() method allows us to search through the descendants of these elements in the DOM tree and construct a new jQuery object from the matching elements. The .find() and .children() methods are similar, except that the latter only travels a single level down the DOM tree.
Source: http://api.jquery.com/find/