user3777827
user3777827

Reputation: 344

How to get the class of a div on click of a button?

I want to hide the div with the class name "myclass" on the click of a button in that div. Please remember there are more than two div with "myclass" name with different id d25, d26. And I want to hide only that div whose cancel button is clicked . How can I do this ! Any help will be appreciated. The code I am using is:

<div class="myclass" id="d25" >

<form method="post" action="#" >
<input type="email" name="emaild" id="emailid"  />
<input type="submit" value="Send" name="sub"/>
<input type="button" value="Cancel" id="cancel" class="can"  />
</form>

</div>

<div class="myclass" id="d26" >

    <form method="post" action="#" >
    <input type="email" name="emaild" id="emailid"  />
    <input type="submit" value="Send" name="sub"/>
    <input type="button" value="Cancel" id="cancel" class="can"  />
    </form>

    </div>


<script>

$(document).ready(function(){
    $(".can").click(function(){


        //what code I need to put here to hide div myclass on the click of button with class "can"

        });
});
</script>   

Upvotes: 1

Views: 832

Answers (7)

ebiv
ebiv

Reputation: 325

Find the div from the buttons parents using the following and hide it.

$(this).parents('div').hide();

The code will be

$(document).ready(function(){
    $(".can").click(function(){
       $(this).parents('div').hide();
    });
});  

http://jsfiddle.net/6nrc8/`

Upvotes: 0

Alex Char
Alex Char

Reputation: 33218

js

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

fiddle

Upvotes: 0

Bhojendra Rauniyar
Bhojendra Rauniyar

Reputation: 85545

You can use parents

$('.can').click(function(){
     $(this).parents('.myclass').hide();
});

Upvotes: 1

Vicky
Vicky

Reputation: 613

You can use closest() jquery function e.g.

 $('.can').click(function(){

           $(this).closest('.myclass').hide();
 });

Upvotes: 0

Rigin
Rigin

Reputation: 187

In you function , try this :

$(this).parents('div').fadeOut();

parents('div') -> here you can specify your class name also

Rigin

Upvotes: 0

xrcwrn
xrcwrn

Reputation: 5325

 $(document).on('click', '.can', function(event) {
                event.preventDefault();
                $(this).closest('.myclass').hide();
            });

Upvotes: 1

Bhushan Kawadkar
Bhushan Kawadkar

Reputation: 28513

Try this : User .closest() to get myClass div in its upper hierarchy.

<script>

$(document).ready(function(){
    $(".can").click(function(){
       $(this).closest('.myclass').hide();
     });
});
</script>

DEMO

Upvotes: 1

Related Questions