Reputation: 344
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
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();
});
});
Upvotes: 0
Reputation: 85545
You can use parents
$('.can').click(function(){
$(this).parents('.myclass').hide();
});
Upvotes: 1
Reputation: 613
You can use closest() jquery function e.g.
$('.can').click(function(){
$(this).closest('.myclass').hide();
});
Upvotes: 0
Reputation: 187
In you function , try this :
$(this).parents('div').fadeOut();
parents('div') -> here you can specify your class name also
Rigin
Upvotes: 0
Reputation: 5325
$(document).on('click', '.can', function(event) {
event.preventDefault();
$(this).closest('.myclass').hide();
});
Upvotes: 1
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>
Upvotes: 1