views:

263

answers:

4

I have a series of Delete icons displays as such:

echo '<img id="'.$items['id'].'" src="images/delete.png" />';

Where $items['id'] is a reference to the id of the DB item I would like that icon to delete.

How can I access this id in jquery when the item is clicked so I can then call a:

$.get('ajax/delete.php', { id: $('#id').val() },      
function(data) {}

Or something of the sort.

+4  A: 

The following code will help you out. Add the class "delete" to each of your img.

$('.delete').onclick(function() {
    var id = $(this).attr('id');
    $.post('ajax/delete.php', { id: id } function(data) {

     ...
    });
});
Alec Smart
So.. onclick of .delete class get the id of whatever item is clicked and put it into the $.get() ?
ian
yes. you can try alert(id) to see what you getting also.
Alec Smart
Also, please always use POST for such tasks.
Alec Smart
POST POST POST. ok ok I will thanks. :)
ian
Note that this will only work if the ids in question are non-numeric. HTML id attributes need to start with a letter. To avoid having to resort to string formatting/encoding I think it's better to store the id as the value of an associated input element.
tvanfosson
for numeric, simply make the id something like id="del_4" etc. In php, all you have to do is $id = substr($_POST['id'],4)
Alec Smart
ian
A: 
$("#imageID").click ( function () {
    alert ( $(this).attr ( "id" ) );
});

if you have a class name

$(".yourclassname").click ( function () {
    alert ( $(this).attr ( "id" ) );
});
rahul
That's a very inefficient way of doing things. You'll have to explicitly add an event handler to each element.
Justin Johnson
Needs to be dynamic but I get the idea... I can do a $("class_name").click instead right?
ian
A: 

I would handle this slightly differently. I would have each delete icon followed by a hidden input that contains the value of the item corresponding to the id of the item to be deleted. I would then use jQuery to extract the value attribute of the hidden field. This way, you won't have to do any magic string manipulation to use numeric ids.

Also, never use a get request to do a delete. All some one has to do is type a URL in the proper format, with the proper credentials to bypass any client-side processing you've added to validate your deletion. AJAX allows you to send other types of requests and you should prefer a POST or DELETE request for deletes.

echo '<img class="deleteIcon" src="images/delete.png" />';
echo '<input type="hidden" value="' + $item[id] + '" />';

$('.deleteIcon').click( function() {
     var id = $(this).next('input[type=hidden]').attr('value');
     $.ajax({
          url: 'ajax/delete.php',
          type: 'delete',
          data: { id: id },
          dataType: 'json', // get back status of request in JSON
          success: function(data) {
                if (data.status) {
                   // remove row, or whatever
                }
                else {
                   // handle deletion error
                },
          ... other parameters/callbacks ...
     });
});
tvanfosson
So you use $this().next to get the id from the hidden input item that follows the item clicked. What is the benefit here from the ('.class_name').click getid of this method?
ian
The benefit is that you can store the data as the value to the input regardless of its format. If you have numeric ids, you'll need to prepend a string to them for them to be valid HTML element ids, then you need to extract the string when you want to use it. Also, you can't re-use non-numeric ids elsewhere without resorting to the string prefix magic. By using relative DOM traversal and storing data as data rather than encoded in an id, you can include the data (id) once and look it up for the various related actions to that data.
tvanfosson
Nice. Good to know. Seems like it would make things cleaner over all. No need for creating things like echo 'del_'.$row['value']'; and then getting rid of del_ again and all that type of hassle.
ian
A: 

I'd personally go for hijax and graceful degradation by wrapping the <img>s with <a>. Plus you most likely shouldn't do any delete/remove operations with GET. But here you go;

$('img[src=images/delete.png]').click(function(e) {
  $.get('ajax/delete.php', { id: this.id }, function() {
    // complete, perhaps get rid of the <img> now?
  });

  return false;
});
muffi