views:

60

answers:

3

This is my HTML:

<ol id="front-page">
<li>
    <img src="images/defaultImage.gif" alt="Default Image" />
    <a href="#" title="Title Entry">Title Entry</a>    </li>
<li>
    <img src="images/defaultImage.gif" alt="Default Image" />
    <a href="#" title="Title Entry">Title Entry</a>
</li>
<li>
    <img src="images/defaultImage.gif" alt="Default Image" />
    <a href="#" title="Title Entry">Title Entry</a>
</li>
<li>
    <img src="images/defaultImage.gif" alt="Default Image" />
    <a href="#" title="Title Entry">Title Entry</a>
</li>
<li>
    <img src="images/defaultImage.gif" alt="Default Image" />
    <a href="#" title="Title Entry">Title Entry</a>
</li>
<li>
    <img src="images/defaultImage.gif" alt="Default Image" />
    <a href="#" title="Title Entry">Title Entry</a>
</li>
<li>
    <img src="images/defaultImage.gif" alt="Default Image" />
    <a href="#" title="Title Entry">Title Entry</a>
</li>
<li>
    <img src="images/defaultImage.gif" alt="Default Image" />
    <a href="#" title="Title Entry">Title Entry</a>
</li>

and my jQuery:

$(document).ready(function() {
$('ol#front-page').find('li:eq(4)').css("margin","0");
$('ol#front-page').find('li:lt(4)').append("<span>New</span>");

});

and what I am trying to do is have the first 3 list items to have a span appended to them. It will append the span to the 0 element but not the other 3?

Thanks for the help!

A: 

Your code appends the span after each one of the first 4 li elements. Is that what you are trying to do?

kgiannakakis
I need it to append it within the li element for example:<li> <span>New</span> <img src="images/defaultImage.gif" alt="Default Image" /> <a href="#" title="Title Entry">Title Entry</a> </li><li>Thanks!
Chris
A: 

Check here (http://jsbin.com/upute) for a demonstration what your code actually does.

This sets margin to 0 on the fifth li (as the list is zero based)

$('ol#front-page').find('li:eq(4)').css("margin","0");

This appends (==at the end) the span for the first four li's

$('ol#front-page').find('li:lt(4)').append("<span>New</span>");

What you probably want to do is this. Set margin to 0 on the fourth element and prepend the span to the first four li's.

$('ol#front-page').find('li:lt(4)').prepend("<span>New</span>");
jitter
Same as above, only the 0 position is getting the span appended to it. Thanks!
Chris
Ok so `lt(4)` and `eq(4)` are ok. Did you check the sample on I linked to in my answer. Doesn't that work for you neither? What browser + jQuery version are you using.
jitter
A: 

Replace 'li:lt(4)' with 'li:lt(3)'. The :lt() filter works on a zero-based index, so :lt(4) means elements 0, 1, 2 and 3.

Reference: Selectors/lt @ jQuery API

P.D. You probably want 'li:eq(3)' as well

Álvaro G. Vicario
li:eq(4) is being used since there are 4 list elements to a row, all with 20px of left margin (accept the first-child). So the targeted element is the visual 5th element (or position 4 in js) to prevent the new row from having a left margin on it's starting list item.li:lt(3) returns the same result, only getting position 0 to have the span appended to it.Thanks!
Chris