Friday 10 August 2018

css - jQuery selector with nth-of-type()



Using Chrome Development Tools




jQuery('.proejct-text-field')


gives me four instances of HTML Elements:



...<>
...<>
...<>
...<>



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.


Answer



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 like



$('.proejct-text-field').first();


You cannot select nth-of-class because there's no such way to select nth element with a class, so your selector should work though it doesn't because you have to take out the period . from the class name in your DOM)*



Demo




Demo 2 (Using jQuery .first())


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...