PDA

View Full Version : jQuery Alternating appends...


many_tentacles
02-12-2010, 05:10 PM
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...

<!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

VIPStephan
02-13-2010, 03:52 AM
How about:

$(document).ready(function(){
$('div p:odd').append('even');
$('div p:even').append('odd');
});


?

many_tentacles
02-15-2010, 09:30 AM
nope... same result...

Spudhead
02-15-2010, 11:45 AM
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:

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

many_tentacles
02-15-2010, 12:08 PM
perfect... thanks