views:

16

answers:

1

I have this code in html (notice the numers 1 and 2in the outside and inside elements):

<div class="list_item" id="item_1">
<div class="list_item_int">My first line</div>
<div class="eval_buttons">
    <div class="approve" id="1"></div>
    <div class="dismiss" id="1"></div>
</div>

<div class="list_item" id="item_2">
<div class="list_item_int">My second line</div>
<div class="eval_buttons">
    <div class="approve" id="2"></div>
    <div class="dismiss" id="2"></div>
</div>

Then I have this script:

$().ready(function(){
    $(".approve").click(function(){
        $.post(
            "php/judge_work.php",
            {action : "1", work_info : this.id},
            function(data){
                $("#item_" + this.id).hide("slow");
            }
        );
    });
    $(".dismiss").click(function(){
        $.post(
            "php/judge_work.php",
            {action : "0", work_info : this.id},
            function(data){
                alert(data);
            }
        );
    });
});

Is there a way to make $("#item_" + this.id).hide("slow"); work? thanks on your kind response.

A: 

You can use .closest(), like this:

$(this).closest(".list_item") //gets the ancestor <div class="list_item">

In your case since this won't be the clicked element in the $.post() callback, it'll look like this:

$(".approve").click(function(){
   var li = $(this).closest(".list_item");
   $.post(
        "php/judge_work.php",
        {action : "1", work_info : this.id},
        function(data){
            li.hide("slow");
        }
   );
});
Nick Craver
Thank You, it worked perfectly. I think that solves my problem. I didn't know about closest, but it's a perfect solution.
Andres Bedoya
@Andres - welcome :) something else to keep in mind, your number IDs are invalid (except in HTML5) and definitely shouldn't be duplicated...I'd use another attribute like `data-id="2"`, etc.
Nick Craver
Well, I changed the numeric ids to id="approved_#" (where # is replaced by the correct number with PHP) and in the receiving function i just used explode('_', $_POST['work_info']);
Andres Bedoya
@Andres - Use something else like `data-id="#"` and `$(this).attr("data-id")` to get it when posting :)
Nick Craver
Hmmm, ok I will, but i still don't know what's the problem with the Ids? aren't they supposed to be unique identifiers of an element in HTML? I have been using id="some_stuff" for a long time and it works. Is there a special reason to avoid using id="some_other_stuff", what makes it invalid? Thanks :D and sorry for asking too much.
Andres Bedoya
@Andres - *Those* IDs aren't invalid, the `id="2"` for example are, because they're not unique and they start with a number...though in practice the not-being-unique is the main issue.
Nick Craver
But (and this is my last question), i can add any attribute to an HTML tag that i wish (except reserved words)? I mean, i can do something like: <div id="unique_id" special_info="info_to_be_sent_to_the_form"></div>
Andres Bedoya
@Andres - I would use `data-` as the prefix, they're called data attributes...they don't interfere in HTML4 and they're specifically reserved for exactly this purpose in HTML5, you can read more here: http://ejohn.org/blog/html-5-data-attributes/
Nick Craver