0

Part of my my PHP:

echo '<li rel="'.$rs['pic_id'].'"><a>'.$rs['name'].'</a> <a class="delete">Delete</a></li>';

jQuery:

$('#taglist li a.delete').bind('click', function(){
        pic_id = $(this).attr("rel");
        $.post('deletetag.php', pic_id, function(){
            viewtag();
          });
    });

The button is not working, as in it didn't get converted into a button although i clearly included the 'a' tags in the html. Where is the problem?

Cole
  • 459
  • 2
  • 6
  • 14
  • Well first start by changing `.bind` to `.on` .on has replaced .bind. – Kolby Feb 04 '13 at 07:24
  • is better to use `on` or `click`. see http://stackoverflow.com/questions/8065305/whats-the-difference-between-on-and-live-or-bind – pbaris Feb 04 '13 at 07:27

3 Answers3

1

Why not just do.

$('#taglist li a.delete').click(function()
{
//Code Here
}));
AshotN
  • 395
  • 4
  • 15
  • Then take a look at your html output. Also use the code I gave you. If that doesn't work it's not js that's the problem it's your HTML structure. – AshotN Feb 04 '13 at 07:25
0

if you are using jQuery 1.7+

try this

echo '<li rel="'.$rs['pic_id'].'"><a href="#">'.$rs['name'].'</a> <a href="#" class="delete">Delete</a></li>';

$("#taglist li a.delete").on("click", function(event){
    event.preventDefault();
    pic_id = $(this).attr("rel");
    $.post('deletetag.php', pic_id, function(){
        viewtag();
    });
});

or

$("body").on("click", "#taglist li a.delete", function(event){
    event.preventDefault();
    pic_id = $(this).attr("rel");
    $.post('deletetag.php', pic_id, function(){
        viewtag();
    });
});
Pragnesh Chauhan
  • 8,363
  • 9
  • 42
  • 53
0

not sure what you are tryin to do but your selector is button here and the attr rel is of parent <li>.. so use parent()...

try this

$('#taglist li a.delete').on('click', function(){
    pic_id = $(this).parent().attr("rel");
    $.post('deletetag.php', {pic_id:pic_id}, function(){
        viewtag();
      });
});

and if your <li> is added dynamically then use on delegate event..

$('#taglist').on('click','li a.delete', function(){....

you can go through the link if you want to read more about jquery on()

bipen
  • 36,319
  • 9
  • 49
  • 62