views:

261

answers:

6

I have a ul list that is loaded by an included PHP file. Those items have a "Delete" link that removes them from the list and the database.

When an item is removed from the center of the list using this code:

$list.find('#city_' + $id).remove();

It leaves a space in the list like this:

alt text

What can I do to refresh that list, without going back to the database and reloading the entire thing?

EDIT

Here is the example code:

<ul id="city_list">
    <li id='city_7'>Eureka - <a href='city.modify.php?id=7&modification_type=edit'>Edit</a> - <a href='#' delete_id='7' class='confirm_delete'>Delete</a></li> <br />
    <li id='city_8'>Rolla - <a href='city.modify.php?id=8&modification_type=edit'>Edit</a> - <a href='#' delete_id='8' class='confirm_delete'>Delete</a></li> <br />

    <li id='city_10'>Union - <a href='city.modify.php?id=10&modification_type=edit'>Edit</a> - <a href='#' delete_id='10' class='confirm_delete'>Delete</a></li> <br />

+1  A: 

I'm guessing your HTML looks like this:

<li><div id="city01">Eureka - <a...>Edit</a> - <a...>Delete</a></div></li>

In this case you would need to remove the parent of the selector

$list.find('#city_' + $id).parent().remove();
fudgey
Close, actually the li is what has the id. That was why I was confused, I would have thought that the gap would not be there if the li was removed.
Pselus
Are you sure the li element was removed?
Dave Van den Eynde
This sent me in the right direction. The li was removed, but the <br /> that I was adding to the code before I converted it to an li wasn't being removed (and in reality shouldn't have even been in the code anymore).
Pselus
+1  A: 

You could try this

$list.find('#city_' + $id).hide('fast', function() {
     $(this).remove();
});
Luke101
+1  A: 

$id holds a simple value, not a wrapped set? .. well, ...ok...

do it like this

$('#city_' + $id).parent().remove();

or

$('div:has(#city_' + $id + ')').remove();

you don't need to use $list or $().find() if your html is properly formed w/ unique ids

Scott Evernden
But then I am needlessly traversing more of the DOM than I have to. The $list is already being used elsewhere in the method so reusing it here is the right way to go in my opinion.
Pselus
Where's the div element in the HTML?
Dave Van den Eynde
well I hate to break it to you, but your opinion is wrong. A bare #id without context is THE FASTEST selector that jQuery offers since it maps directly into getElementById() - there is *no traversing* when you supply an #id. By supply $last as a context you are actually slowing the selector down.
Scott Evernden
Please refer to http://www.artzstudio.com/2009/04/jquery-performance-rules/
Scott Evernden
+1  A: 

You could use FireBug to see what you're actually removing, but I'm guessing it's the label element, not the li element. The way you styled it makes it look as if it's an empty space, while I think it's an empty list element.

How about this:

$list.find('li#city_' + $id).remove();
Dave Van den Eynde
+2  A: 

id="city_X" is duplicated on the <li> and the <label>. An id must be unique in the document or you will have undefined behavior when trying to do $('#my_id').

<label>s are for form input elements, not for text display. Either use no tag or <span>

Here is how I would do it:

<ul id="city_list">
    <li id='city_7' class="city">
     <span class="label">Eureka</span>
      - <a href='city.modify.php?id=7&modification_type=edit'>Edit</a>
      - <a href='#' class='confirm_delete'>Delete</a>
    </li> 
    <li id='city_8' class="city">
     <span class="label">Rolla</span>
      - <a href='city.modify.php?id=8&modification_type=edit'>Edit</a>
      - <a href='#' class='confirm_delete'>Delete</a>
    </li>        
    <li id='city_10' class="city">
     <span class="label">Union</span>
      - <a href='city.modify.php?id=10&modification_type=edit'>Edit</a>
      - <a href='#' class='confirm_delete'>Delete</a>
    </li> 
</ul>

Assuming you want to delete when clicking on the delete link.

// Use event delegation
$('#city_list').bind('click', function(event)
{
    $(event.target).closest('.confirm_delete').each(function()
    {
     // Get ID from city
     var $city = $(this).closest('.city');
     var id = $city.attr('id').match(/city_(.+)/)[1];

     // Do any AJAX request to tell the server to delete the city

     $city.remove();
    })
})
Vincent Robert
Sorry that your response was posted after I discovered the issue because it is a great response. Sadly the issue was one of poor programming practices and not syntax related.However, I really like the code you provided and I think I'll be modifying my code to work more like this. (I was using hidden textboxes to store id's because I don't know Regular Expressions)
Pselus
+1  A: 
$list.find('#city_' + $id).next().remove() /* remove the <br/> */
     .end() /* go back to what was found originally */
     .remove(); /* and remove */
prime_number