I'm writing a html page where there are 1 different jquery operations done.
- Selecting dates from jquery datepicker plugin.
- Getting data from database for auto complete.
And below is my code.
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Autocomplete in java web application using Jquery and
JSON</title>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script src="autocompleter.js"></script>
<link rel="stylesheet"
href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="header">
<h3>Autocomplete in java web application using Jquery and JSON</h3>
</div>
<br />
<br />
<div class="search-container">
<div class="ui-widget">
<input type="text" id="search" name="search" class="search" />
</div>
</div>
<p>
Date: <input type="text" id="startDatePicker">
</p>
<p>
Date: <input type="text" id="endDatePicker">
</p>
<div id="resultarea"></div>
</body>
</html>
autocompleter.js
$(document).ready(function() {
$(function() {
$("#startDatePicker").datepicker();
$("#endDatePicker").datepicker();
});
});
$(document).ready(function() {
$(function() {
$("#search").autocomplete({
source : function(request, response) {
$.ajax({
url : "Controller",
type : "GET",
data : {
term : request.term
},
dataType : "json",
success : function(data) {
response(data);
}
});
}
});
});
});
Here, fetching the data for displaying name is working fine, but the datepicker is not working as expected. But when i use the below code. (Removing the datepicker from and pasting it in index.html header's script tag). The datepicker is working fine.
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Autocomplete in java web application using Jquery and
JSON</title>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script src="autocompleter.js"></script>
<link rel="stylesheet"
href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<link rel="stylesheet" href="style.css">
<script>
$(function() {
$("#startDatePicker").datepicker();
$("#endDatePicker").datepicker();
});
</script>
</head>
<body>
<div class="header">
<h3>Autocomplete in java web application using Jquery and JSON</h3>
</div>
<br />
<br />
<div class="search-container">
<div class="ui-widget">
<input type="text" id="search" name="search" class="search" />
</div>
</div>
<p>
Date: <input type="text" id="startDatePicker">
</p>
<p>
Date: <input type="text" id="endDatePicker">
</p>
<div id="resultarea"></div>
</body>
</html>
I'm unable to know where am I going wrong. And why the second code works and why doesn't the first.
One of my teachers suggested that making HTML in plain and linking all the css and js would be a good design approach, So I wanted to separate the js file from HTML.
Thanks
$(document).ready(...autocompleter.jshas two$(document).ready(function(){});where only one should exist for the entire document. This function is the equivalent of saying do this when the page loads, but you have it twice, that is a conflict of interest, you should only need one and put all your code in there<script>tags should be written after your<body>tag