tags:

views:

75

answers:

2

i have something similar to the following... used a python script to JSON to populate.

<ul>
<li id="p1" class="x">val1 <img id="ip1" class="redx" src="redx.gif"/></li>
<li id="p2" class="x">val2 <img id="ip2" class="redx" src="redx.gif"/></li>
<li id="p3" class="x">val3 <img id="ip3" class="redx" src="redx.gif"/></li>
<li id="p4" class="x">val4 <img id="ip4" class="redx" src="redx.gif"/></li>
</ul>

I'd like some jquery to... when click the red x image.. to hide the whole LI cell it is in.

I have tried a few things but ... to no avail.

EDIT: All in all.. this doesnt seem to trigger ... this seems like a fundamental issue.

$(function(){
    function removeli() {
      alert("got here" + this.id);
    }
    $("redx").click(removeli);
});

Any ideas on the final jquery code to accomplish this?

+2  A: 

need a . prefix for classes

$('.redx').live('click', removeli);

updated code using live:

$(function() {
    $('.redx').live('click', function() {
        $(this).parent('li').hide(); // or .remove()
    });
});

using delegate (will only work if the <ul> doesn't change, only the <li>'s within it do):

$(function() {
    $('ul').delegate('img', 'click', function() {
        $(this).parent('li').hide();
    });
});
Anurag
LOL even that being true. still no go after fixing the syntax. In firebug should i be seeing an onclick added to all of the li's in the code?
Kirby
that is most likely because you are adding the rows dynamically after setting up the click handler. use either `live` or `delegate` to add the event handlers as @Tatu suggested. that will work for newly inserted elements as well.
Anurag
i finally figured out what was wrong w/ TATU's syntax. Thank you for updated. Thank you all for helping! :) Problem solved.
Kirby
A: 
$(".redx").click(function(){
    $(this).parent().hide();
});
Bradley Mountford
the onclick doesnt seem to be firing.
Kirby