Using Chrome Development Tools
jQuery('.proejct-text-field')
gives me four instances of HTML Elements:
<div class=".proejct-text-field ...">...<>
<div class=".proejct-text-field ...">...<>
<div class=".proejct-text-field ...">...<>
<div class=".proejct-text-field ...">...<>
Trying the following doesn't return any Elements.
jQuery('.proejct-text-field:nth-of-type(1)')
As for my understanding the first Element should be returned. I just use jQuery in order to find the right selector for my purposes and to take them into my css file. So How can I select the first Elment of those Divs. Btw: They are not wrapped into a certain parent element. So any child methods won't work. Further the number of divs is variable.
First change mark up with this
instead
And use
.first()
if you want firstFirstly, you have a
.
in your class Name, that's unnecessary... Just make itproejct
Instead ofnth-of-child
, use:eq
, like thisand your jQuery
or
Your
class
name is incorrect, you are having a.
in the.class=".proejct-text-field ..."
Also, you can use
.first()
to select the very first element likeYou cannot select
nth-of-class
because there's no such way to selectnth
element with aclass
, so your selector should work though it doesn't because you have to take out the period.
from theclass
name in your DOM)*Demo
Demo 2 (Using jQuery
.first()
)The
:nth-of-type()
selector "Selects all elements that are the nth child of their parent in relation to siblings with the same element name." - But you indicated that your elements are "not wrapped into a certain parent element", i.e., they have no particular relationship with each other.If you just want the first element with that class do this:
Note that you should remove the
.
from the class name in yourclass=""
attribute in the markup. I.e., it should be:(And you've spelled "project" incorrectly everywhere.)
The nth-of-type selector looks for element type, like
div, span
etc not for class name or any other selectorsuse :eq(index)
or .eq(index)