How do I replace text from the 3rd element of a list of 10 items?

jQuery

How do I replace text from the 3rd element of a list of 10 items?

Either the :eq() selector or the .eq() method will allow you to select the proper item. However, to replace the text, you must get the value before you set it:

1
2
3
4
5
6
7
8
9
                          
// This doesn't work; text() returns a string, not the jQuery object
$( this ).find("li a").eq( 2 ).text().replace( "foo", "bar" );
// This works
var $thirdLink = $( this ).find("li a").eq( 2 );
var linkText = $thirdLink.text().replace( "foo", "bar" );
$thirdLink.text( linkText );

The first example just discards the modified text. The second example saves the modified text and then replaces the old text with the new modified text. Remember, .text() gets; .text("foo") sets.