• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 218
  • Last Modified:

jQuery: Third list items

Using jQuery I want to alert the contents of the 3rd list item:

Like this:
alert('<a  href="http://www.example.com/"><img src="http://www.example.com/example-03.jpg" alt="Example" width="210" /></a>');

This does NOT work:
alert($("#mycarousel li").3.html());



<html> 
<head> 
<script language="javascript" src="http://code.jquery.com/jquery-1.4.2.min.js" type="text/javascript"></script> 
<script language="javascript"> 
$(document).ready(function() { 
        alert($("#mycarousel li").length); 
}); 
</script> 
</head> 
<body> 
<div class="gallery"> 
<ul id="mycarousel" class="jcarousel-skin-atlantica-small"> 
<li> <a  href="http://www.example.com/"><img src="http://www.example.com/example-01.jpg" alt="Example" width="210" /></a></li> 
<li> <a  href="http://www.example.com/"><img src="http://www.example.com/example-02.jpg" alt="Example" width="210" /></a></li> 
<li> <a  href="http://www.example.com/"><img src="http://www.example.com/example-03.jpg" alt="Example" width="210" /></a></li> 
<li> <a  href="http://www.example.com/"><img src="http://www.example.com/example-04.jpg" alt="Example" width="210" /></a></li> 
<li> <a  href="http://www.example.com/"><img src="http://www.example.com/example-05.jpg" alt="Example" width="210" /></a></li> 
<li> <a  href="http://www.example.com/"><img src="http://www.example.com/example-06.jpg" alt="Example" width="210" /></a></li> 
<li> <a  href="http://www.example.com/"><img src="http://www.example.com/example-07.jpg" alt="Example" width="210" /></a></li> 
<li> <a  href="http://www.example.com/"><img src="http://www.example.com/example-08.jpg" alt="Example" width="210" /></a></li> 
<li> <a  href="http://www.example.com/"><img src="http://www.example.com/example-09.jpg" alt="Example" width="210" /></a></li> 
<li> <a  href="http://www.example.com/"><img src="http://www.example.com/example-10.jpg" alt="Example" width="210" /></a></li> 
</ul> 
</div> 
</body> 
</html>

Open in new window

0
hankknight
Asked:
hankknight
  • 2
2 Solutions
 
SaphiraCommented:
Try... alert($("#mycarousel li:nth-child(3)).html()
0
 
hankknightAuthor Commented:
Thanks, but I am getting an error.  Am I doing something wrong?
<html> 
<head> 
<script language="javascript" src="http://code.jquery.com/jquery-1.4.2.min.js" type="text/javascript"></script> 
<script language="javascript"> 
$(document).ready(function() { 

alert($("#mycarousel" li:nth-child(3)).html());

}); 
</script> 
</head> 
<body> 
<div class="gallery"> 
<ul id="mycarousel" class="jcarousel-skin-atlantica-small"> 
<li> <a  href="http://www.example.com/"><img src="http://www.example.com/example-01.jpg" alt="Example" width="210" /></a></li> 
<li> <a  href="http://www.example.com/"><img src="http://www.example.com/example-02.jpg" alt="Example" width="210" /></a></li> 
<li> <a  href="http://www.example.com/"><img src="http://www.example.com/example-03.jpg" alt="Example" width="210" /></a></li> 
<li> <a  href="http://www.example.com/"><img src="http://www.example.com/example-04.jpg" alt="Example" width="210" /></a></li> 
<li> <a  href="http://www.example.com/"><img src="http://www.example.com/example-05.jpg" alt="Example" width="210" /></a></li> 
<li> <a  href="http://www.example.com/"><img src="http://www.example.com/example-06.jpg" alt="Example" width="210" /></a></li> 
<li> <a  href="http://www.example.com/"><img src="http://www.example.com/example-07.jpg" alt="Example" width="210" /></a></li> 
<li> <a  href="http://www.example.com/"><img src="http://www.example.com/example-08.jpg" alt="Example" width="210" /></a></li> 
<li> <a  href="http://www.example.com/"><img src="http://www.example.com/example-09.jpg" alt="Example" width="210" /></a></li> 
<li> <a  href="http://www.example.com/"><img src="http://www.example.com/example-10.jpg" alt="Example" width="210" /></a></li> 
</ul> 
</div> 
</body> 
</html>

Open in new window

0
 
SaphiraCommented:
Sorry, my mistake, I forgot to close the " in the right place
alert($("#mycarousel li:nth-child(3)").html()

Open in new window

0
 
remorinaCommented:
The correct structure would be
alert($("#mycarousel li:nth-child(3)").html());

Open in new window

0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now