0

I don't know what's wrong, i have as follows:

<tr>
  <td><span class="idTask">1</span></td>               
  <td><span>Another Hello</span></td>                              
  <td><img class="delete" src="img/delete.png" alt="Edit" /></td>
  <td><input type="checkbox" name="delete[]" value="" /></td>
</tr>

When i click a button a function calls to iterate through the all checked box for being delete, i need to retrieve the value from idTask, i'm trying to do as follows:

$(function(){
    $(".deleteAll").click( function(){
    var conf=confirm("Are you sure?");
    if(conf==true) {  
        $(':checkbox').each(function () {
        if(this.checked) {
            alert(($(this)).parent().find('.idTask').text());
        } 
        });
    }
    });
});

But doesn't work, with prev() or next() works, but is not for me an elegant way to do this, any suggestions for find a children element matching his class inside a parent to retrieve his value?

1 Answer 1

1

The parent of the checkbox is <td> but not <tr>. You should better use closest():

$(".deleteAll").click(function() {
    if (confirm("Are you sure?")) {
        $(":checkbox").each(function() {
            if (this.checked) {
                // --------------.-.-.-.-.-.-.
                //               v v v v v v v
                var id = $(this).closest("tr").find(".idTask").text();
                alert(id);
            }
        });
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

Wow, thanks VisioN! You save my day! I use before closest, but i was wrong giving "td" instead "tr" closest suppose to find the all elements that match with the checkbox in this case, and search for his childrens, right? I think i misunderstanding some concepts.
Easily speaking, closest finds the first element that matches the selector within all parents of your checkbox in the DOM tree. So in our case it will find the first tr.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.