javascript - Hiding the closest div with the specified class - Stack Overflow

I'm working on an application in which mimics desktop style application windows that open, minimiz

I'm working on an application in which mimics desktop style application windows that open, minimize and close. I've managed to get them to expand and collapse, but I am unable to get the to close, and I can't figure out why. I need to be able to close the current div by clicking a button, see code / eg. below.

<script>
     $(document).ready(function () {

        $("formmon").first().show();
             $(".expand").click(function () {
                 $(this).parents().next("formmon").show();
             })
             $(".collapse").click(function () {
                 $(this).parents().next("formmon").hide();
             });
             $(".close").click(function () {
                 $(this).parents().next("div.module").hide();
             });
    });
 </srcipt>
 <div id="task_manager" class="module"> <!-- Need the x with class close to hide this div-->
 <h3>Task Manager</h3> <!-- This header and below icons are shown when minimized-->
<div class="module_actions">
    <span class="icons_small right close">X</span>
    <span class="icons_small right collapse">-</span>
    <span class="icons_small right expand">+</span> 
</div>  
<form class="mon">
    <fieldset>
     <legend> Some Titlee/legend>
    </fieldset>
</form>
</div>

Can anyone see why this is not hiding the div? THanks,

I'm working on an application in which mimics desktop style application windows that open, minimize and close. I've managed to get them to expand and collapse, but I am unable to get the to close, and I can't figure out why. I need to be able to close the current div by clicking a button, see code / eg. below.

<script>
     $(document).ready(function () {

        $("form.mon").first().show();
             $(".expand").click(function () {
                 $(this).parents().next("form.mon").show();
             })
             $(".collapse").click(function () {
                 $(this).parents().next("form.mon").hide();
             });
             $(".close").click(function () {
                 $(this).parents().next("div.module").hide();
             });
    });
 </srcipt>
 <div id="task_manager" class="module"> <!-- Need the x with class close to hide this div-->
 <h3>Task Manager</h3> <!-- This header and below icons are shown when minimized-->
<div class="module_actions">
    <span class="icons_small right close">X</span>
    <span class="icons_small right collapse">-</span>
    <span class="icons_small right expand">+</span> 
</div>  
<form class="mon">
    <fieldset>
     <legend> Some Titlee/legend>
    </fieldset>
</form>
</div>

Can anyone see why this is not hiding the div? THanks,

Share Improve this question asked Sep 21, 2013 at 18:39 MarkMark 4,8738 gold badges57 silver badges93 bronze badges 3
  • Do you see any errors in the console? Other wise your code should work. Also by the way use closest(''.module_actions') instead of parents() – PSL Commented Sep 21, 2013 at 18:43
  • @PSL, there were no console errors but using .closest worked for me, thanks!!! – Mark Commented Sep 21, 2013 at 18:48
  • in that case you have more code than what you showed.. That is why parents() did not work for you. – PSL Commented Sep 21, 2013 at 18:50
Add a ment  | 

3 Answers 3

Reset to default 3

The answer is in the question:

$(".close").click(function () {
    $(this).closest("div.module").hide();
});

Demo fiddle

Also you should change your calls to parents() to just parent() so you just go up one level in the DOM tree

You had a missing < in <legend> Some Titlee/legend>, after that it works.

Check here

This should work :

  $(".close").click(function () {
       $(this).parent().hide();
   });

JSFiddle
Doc : parent()

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745578165a4634092.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信