Correct syntax for jQuery inside an 'if' statement?
Hi folks,
I'm new to javascript, jQuery and the forum - so this may well be a stupid question...
The following script allows a control with ID = 'next' to move through a list of sibling elements (class = 'ListItems') by controlling which ListItem is allocated ID = 'vis'. The script works until the visible ListItem is the last element.
So, I introduced line 4, which attempts to prepare the first ListItem to be the next to display, when the current visible item is the last in the list. But the syntax is wrong, as the script is ignored with this line in play.
Any clues gratefully received.
Best,
Keith..
- <script type="text/javascript">$("#next").click(function(){
- //Prepare next ListItem
- if("#vis").is(".ListItem:last")){(".ListItem:first") .attr("id", "nxt");} //Mark first ListItem as next
- $("#vis").next().attr("id", "nxt"); //Mark next ListItem as next
-
- //Switch visibility
- $("#vis").removeAttr("id"); // Release id="vis" from current visible ListItem
- $("#nxt").attr("id", "vis"); // Change "nxt" next ListItem to "vis" visible
- $(".ListItem").fadeOut(300); //Hide all ListItems
- $("#vis").delay(300).fadeIn(300); // Show new visible ListItem
- });
- </script>