September 12, 2012

jquery nth selector in IE7

Question by Phil

I have a list of elements and need to make some amends to the first element in the list using jquery. I have tried a few methods to target the first item but none of them seem to work in IE7. These are the following methods I’ve tried…

 $(this).eq(1)
 $(this).first()
 $(this:nth-child(1))

All of these methods worked in all browsers except IE7, does anyone know of a fix to use for IE7 or a different method that will work in IE7?

Thanks in advance for any help?

Answer by Pradeeshnarayan

Try like this

$(this:first-child)

Will get more info from here

EDIT

Sorry for the confusion.
What I was trying to say is to use first-child instead of first()

You can use like this $('ul li:first-child').css('background-color', 'red');

Answer by Starx

.first() is a valid function and will work.

For example:

$("li").first(); // will match the first li in the tree

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!