Reputation: 11
I need to hide these divs on click of the button
in another div
as shown below. When I click on a button
in another div
, it hides the other div
and the opposite.
<!DOCTYPE html>
<html lang="en">
<head>
<title>
tesing hide
</title>
<style>
#ticket, #usernamepassword { margin-top: 20px; width: 960px; color: navy; background-color: pink; border: 2px solid blue; padding: 5px; text-align: center; }
</style>
</head>
<center>
<div id="ticket">
<div> this is the content of my ticket div
</div>
<button>Show usernamepassword</button>
</div>
<div id="usernamepassword">
<div> this is the content of username and password div</div>
<button>Show ticket</button>
</div>
</center>
</html>
Upvotes: 1
Views: 87
Reputation: 926
$( ".target" ).hide();
This will hide an element and similarly
$( ".target" ).show();
will show it.
Using these two in a couple of functions and then calling them from click events on the buttons, should give you what you're looking for.
I am not providing the full code solution as this is trivial and you should be able to follow the docs yourself.
Here is a link to the documentation so you can read about it:
Upvotes: 3
Reputation: 58647
Like this?
$('#usernamepassword').hide();
$('#ticket button').on('click', function(){
$('#usernamepassword').toggle();
$('#ticket').toggle();
});
$('#usernamepassword button').on('click', function(){
$('#ticket').toggle();
$('#usernamepassword').toggle();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en">
<head>
<title>
tesing hide
</title>
<style>
#ticket, #usernamepassword { margin-top: 20px; width: 960px; color: navy; background-color: pink; border: 2px solid blue; padding: 5px; text-align: center; }
</style>
</head>
<center>
<div id="ticket">
<div> this is the content of my ticket div
</div>
<button>Show usernamepassword</button>
</div>
<div id="usernamepassword">
<div> this is the content of username and password div</div>
<button>Show ticket</button>
</div>
</center>
</html>
Upvotes: 1
Reputation: 1313
Just add an id to both of the buttons and assign an event to them to toggle the divs.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en">
<head>
<title>
tesing hide
</title>
<style>
#ticket, #usernamepassword { margin-top: 20px; width: 960px; color: navy; background-color: pink; border: 2px solid blue; padding: 5px; text-align: center; }
</style>
</head>
<center>
<div id="ticket">
<div> this is the content of my ticket div
</div>
<button id="showUsr">Show usernamepassword</button>
</div>
<div id="usernamepassword">
<div> this is the content of username and password div</div>
<button id="showTicket">Show ticket</button>
</div>
</center>
</html>
<script>
$('#showUsr').on('click',function(){
$('#usernamepassword').toggle();
});
$('#showTicket').on('click',function(){
$('#ticket').toggle();
});
</script>
Upvotes: 0