0

I have a list:

<ul id="links_list" class="links_list">

<li id="296" class="sidebar_link">text
    <a onclick="deleteLink(296)" href="javascript:void(0);">Delete Link</a>
</li>

<li id="297" class="sidebar_link">text2
    <a onclick="deleteLink(297)" href="javascript:void(0);">Delete Link</a>
</li>

    ... etc for a long list of items ...

</ul>

I'm currently able to remove the first element using this:

function deleteFirst() {

... do ajax stuff ..

$('ul.links_list li:first-child').fadeTo("fast", 0.01, function(){ //fade
    $(this).slideUp("fast", function() { //slide up
        $(this).remove(); //then remove from the DOM
    });

});

}

How can I modify this function to allow me to delete any item in the list?

1
  • For anyone finding this in the future please do not use the accepted answer, $("anything #id") is a tremendously inefficient selector, and doesn't address any of the issues here. An ID selector should be only $("#ID"). Commented Sep 27, 2010 at 13:23

4 Answers 4

2

Instead of $('ul.links_list li:first-child') use $('ul.links_list li#' + itemID) and pass the itemID through the delete function.

Sign up to request clarification or add additional context in comments.

2 Comments

You should never use an ID selector like that it should just be $('#' + itemID).
Nick Craver is right, if using IDs there is no need to be so specific with the selector.
1

I would bind all the links at once instead of inline, like this:

<ul id="links_list" class="links_list">
  <li data-id="296" class="sidebar_link">text
    <a href="#">Delete Link</a>
  </li>
  <li data-id="297" class="sidebar_link">text2
    <a href="#">Delete Link</a>
  </li>
</ul>

Then script like this:

$('#links_list li a').click(function(e) {
  var id = $(this).closest("li").attr("data-id");
  //do ajax stuff
  $(this).closest("li").fadeTo("fast", 0.01).slideUp("fast", function() {
    $(this).remove();
  });
  e.preventDefault(); //prevent page scrolling with # href
});

This fixes a few issues and some improvements:

  • IDs can't start with a number unless you're using HTML5
  • Markup is much lighter (you can probably remove the classes too)
  • .slideUp() is a queued animation same as .fadeTo(), no need to use a callback for it
  • The ID is gotten relatively, no more in-line script, easier to maintain and in another file

You can test it out here.

2 Comments

"IDs can't start with a number unless you're using HTML5" Well yeah, but then you're using data-id, which is also HTML5, so erm... what was my point again? Never mind.
@YiJiang - data attributes don't interfere with HTML4, where as in numeric IDs are explicitly not allowed. One is "just isn't in the spec yet" the other is specifically forbidden :)
0

:nth-child Selector

See http://api.jquery.com/nth-child-selector/

For example :

$('ul.links_list li:nth-child(10)')

Comments

0

You could change your HTML to pass a reference to the one that was clicked, then use that reference.

HTML

<li id="296" class="sidebar_link">text
    <a onclick="deleteLink(this)" href="javascript:void(0);">Delete Link</a>
</li>
<li id="297" class="sidebar_link">text2
    <a onclick="deleteLink(this)" href="javascript:void(0);">Delete Link</a>
</li>

javascript

function deleteLink( elem ) {
    $( elem.parentNode ).fadeTo("fast", 0.01, function() { //fade
        $(this).slideUp("fast", function() { //slide up
            $(this).remove(); //then remove from the DOM
        });
    });
}​

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.