I put my $(document).ready() code in the HEAD of the document, and inside it I have alert($("#some-textbox").val()), but this produces “undefined” when the page loads. However, If I move this code to the footer of the page, it works.
Code:
<head>
...
<script type="text/javascript">
var searchForm = $("#search-form");
var searchBar = $("#search-bar");
var INITIAL_TEXT = "Start typing to find friends or colleagues...";
$(document).ready(function() {
alert(searchBar.val()); // For testing purpose only; outputs undefined
searchForm.submit(function(e) {
if(searchBar.val() == INITIAL_TEXT) {
searchBar.val("");
}
});
searchBar.focus(function() {
if(searchBar.val() == INITIAL_TEXT) {
searchBar.val("");
}
});
searchBar.blur(function() {
if(searchBar.val() == "") {
searchBar.val(INITIAL_TEXT);
}
});
});
</script>
You are fetching the object outside the
doucment.readywhen the DOM hans’t loaded yet.move the entire code into the function.