Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Dec 2005
    Location
    UK
    Posts
    207
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Alternating appends...

    Hi

    I have 2 divs with the numbers 1-3 in them.

    I then use jquery to specify whether the numbers are odd or even...

    Trouble is it does not reset the odds/evens when it starts the second div.


    Here's the code, hope you know what I mean...

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Untitled Document</title>
    
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
    <script type="text/javascript">
    	$(document).ready(function(){		
        	$('p:odd').append('even');
    		$('p:even').append('odd');
    	});
    </script>
    </head>
    
    <body>
    
    <div>
    <p>1 </p>
    <p>2 </p>
    <p>3 </p>
    </div>
    
    <div>
    <p>1 </p>
    <p>2 </p>
    <p>3 </p>
    </div>
    
    </body>
    </html>
    Thanks

  • #2
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,642
    Thanks
    6
    Thanked 1,005 Times in 978 Posts
    How about:
    Code:
    $(document).ready(function(){		
        	$('div p:odd').append('even');
    	$('div p:even').append('odd');
    });
    ?

  • #3
    Regular Coder
    Join Date
    Dec 2005
    Location
    UK
    Posts
    207
    Thanks
    6
    Thanked 2 Times in 2 Posts
    nope... same result...

  • #4
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts
    I think you'll need to apply it to each div, otherwise it's looking for p tags within the scope of the whole document:

    Code:
    $('div').each(function(){
    	$(this).find('p:odd').append('even');
    	$(this).find('p:even').append('odd');
    });

  • Users who have thanked Spudhead for this post:

    many_tentacles (02-15-2010)

  • #5
    Regular Coder
    Join Date
    Dec 2005
    Location
    UK
    Posts
    207
    Thanks
    6
    Thanked 2 Times in 2 Posts
    perfect... thanks


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •