Toto Toto - 1 year ago 90
jQuery Question

How to properly combine selectors with Javacript identifiers?

I have selected a DOM element in Javasacript.

var myParentElement = document.getElementById('myParentId');

Now, let's say I want, using jQuery to select the first element which is child of that one, is a visible, enabled input element, AND have a specific value for a given attribute (here and I want to check the element name).

$([name="myChildName"]).children(':input:enabled:visible:first'); // ?

Above snippet is incorrect, how should I do that ? (I know a bit of CSS but not much jQuery).

PS. I am limited to jQuery 1.7.2

EDIT : sorry, actually was a tiny bit more complicated than the first line, as I have a name, not an ID.
Anyway, thanks to your advice I have found the solution I was looking for:
$("[name=' + myNameIdentifier +']").filter(":input:enabled:visible:first");

Additionally, due to my CSS being rusty, I was confused about children, find and filter so my question was unclear. I wanted to select all elements with a given name, then filter them.
I did not care about descendant (.find()) or children (.children()).

Answer Source

Use children selector(>)

$('#myParentId > [name="myChildName"]:input:enabled:visible:first')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download