views:

325

answers:

3

So i have a unordered list, each item in the list has a button that is suppose to toggle the post comment textarea. Unfortunately with my first attempt when u click on one Post Comment button all textareas open, then i tried to use this to make sure only one element is selected. Here is the code:

<ul class="todosDisplay">
 <li><span>Content of todo</span><a class="postComment">Post Comment</a>
     <textarea class="showMe"></textarea>
 </li>
<li><span>Content of todo</span><a class="postComment">Post Comment</a>
     <textarea class="showMe"></textarea>
 </li>
<li><span>Content of todo</span><a class="postComment">Post Comment</a>
     <textarea class="showMe"></textarea>
 </li>
 </ul>

And here is my jquery code

$(".postComment").click(function () { 
    $(this).parent().find(".showMe").toggle();
  });

As you can see my poor man's attempt to get to the parent of the ACTUAL element, and then find the element we need to toggle does not work :)

Thanks a lot in advance!

A: 

you can use jQuery's $.closest(".showMe") function.

contagious
A: 

Just built this in Visual Studio and it seems to work. The only thing I noticed with the example above was that the href was missing from the anchor tags resulting in IE not rendering them as links. I added href="#" and your code seemed to work for me. Clicking the link would close the textarea correctly.

<script type="text/javascript">
    $(document).ready(function() {

        $(".postComment").click(function() { $(this).parent().find(".showMe").toggle(); });

    });
</script>

<ul class="todosDisplay">
    <li><span>Content of todo</span><a class="postComment" href="#">Post Comment</a>
        <textarea class="showMe"></textarea>
    </li>
    <li><span>Content of todo</span><a class="postComment" href="#">Post Comment</a>
        <textarea class="showMe"></textarea>
    </li>
    <li><span>Content of todo</span><a class="postComment" href="#">Post Comment</a>
        <textarea class="showMe"></textarea>
    </li>
</ul>
NeilE
It does seem to work... unfortunately i rewrote my actual code so it seems simpler in here, now i guess i have to figure out why it's not working with my big ole complicated version.
Angel Grablev
Ahh yes here is where my problem comes, i actually have<code><li><div class="todos"><a href="#" class="postComment">Post</a></div><div class="comment"><textarea></textarea></div></li></code>
Angel Grablev
A: 

I would suggest changing the jQuery to:

$(".postComment").click(function(){ 
   $(this).siblings(".showMe").toggle();
   return false;
});
Jojo