I have two .on('click')
functions (#addBooksId , #addCategoryId) When I try clicked one of the function in my Document Object Model it replacing the new value using .replaceWith
but when I try to clicked another function in my Document Object Model it doesn't replacing new value. How can I do this without refreshing my browser? Instead of clicking one of the functions and refresh my browser again (repeat) What I want to do every time I clicked the function it's changing the value simultaneously. Here's my code below.
$(document).on("click","#addBooksId",function() {
$.get('addBooks.html', function(data)
{ //The current page
$('#form').replaceWith(data); // Receive with value
alert('This is add books section');
});
});
$(document).on("click","#addCategoryId",function() {
$.get('addCategory.html', function(data)
{ //The current page
$('#form').replaceWith(data); // Receive with value
alert('This is add category section');
});
});
main.html
< script type = "text/javascript"
src = "https://code.jquery.com/jquery-1.12.3.min.js" >
< /script>
$(document).on("click", "#addBooksId", function() {
$.get('addBooks.html', function(data) { / / The current page
$('#form').replaceWith(data); // Receive with value
alert('This is add books section');
});
});
$(document).on("click", "#addCategoryId", function() {
$.get('addCategory.html', function(data) { //The current page
$('#form').replaceWith(data); // Receive with value
alert('This is add category section');
});
});
body {
padding: 0;
margin: 0;
overflow-y: scroll;
font-family: Arial;
font-size: 15px;
}
#container {
background-color: #707070;
}
#menu {
width: 1250px;
margin: 0 auto;
text-align: left;
}
#menu ul {
list-style-type: none;
/*Remove bullets*/
padding: 0;
margin: 0;
position: relative;
}
#menu ul li {
display: inline-block;
}
#menu ul li:hover {
text-decoration: none;
color: black;
}
#menu ul li a,
visited {
color: #CCC;
/*Color of text*/
display: block;
/*Takes up the full width available*/
padding: 15px;
text-decoration: none;
}
#menu ul li a:hover {
color: #CCC;
text-decoration: none;
}
#menu ul li:hover ul {
display: block;
}
#menu ul ul li {
display: block;
}
#menu ul ul {
display: none;
position: absolute;
background-color: #707070;
min-width: 140px;
/*Width when hover*/
}
#menu ul ul li a:hover
/*Color of text when hover*/
{
color: #099;
}
<!doctype html>
<html>
<head>
<title>Main</title>
</head>
<body>
<div id="container">
<div id="menu">
<ul>
<li>
<a href="#">Manage Books</a>
<ul>
<div id="addBooksId">
<li><a href="#">Add Books</a>
</li>
</div>
<div id="addCategoryId">
<li><a href="#">Add Category</a>
</li>
</div>
</ul>
</li>
</ul>
</div>
<!--- end menu --->
</div>
<!--- end container --->
<div id="form">
<p>Hi</p>
</div>
</body>
</html>
addBooks.html
.addBooks {
clear: both;
width: 800px;
margin: 0 auto;
margin-top: 10%;
}
.addBooks label {
float: left;
width: 150px;
text-align: right;
}
.addBooks input {
text-align: left;
margin-left: 100px;
}
<!doctype html>
<html>
<head>
</head>
<body>
<div id="container">
<div class="addBooks">
<h1>Add Books</h1>
<hr>
<form action="#">
<fieldset>
<div class="form-field">
<label for="bookId">Book Id:</label>
<input type="text" id="bookId" name="bookId">
</div>
<br />
<div class="form-field">
<label for="bookName">Book Name:</label>
<input type="text" id="bookName" name="bookName">
</div>
</fieldset>
</form>
</div>
<!--- end of addBooks --->
</div>
<!--- end of container --->
</body>
</html>
addCategory.html
.addCategory {
clear: both;
width: 800px;
margin: 0 auto;
margin-top: 10%;
}
.addCategory label {
float: left;
width: 150px;
text-align: right;
}
.addCategory input {
text-align: left;
margin-left: 100px;
}
<!doctype html>
<html>
<head></head>
<body>
<div id="container">
<div class="addCategory">
<h1>Add Category</h1>
<hr>
<form action="#">
<fieldset>
<label for="categoryName">Category:</label>
<input type="categoryName" id="categoryName" name="categoryName">
<br />
</fieldset>
</form>
</div>
<!--- end of addCategory --->
</div>
<!--- end of container --->
</body>
</html>