I’m new to jQuery and had the following question…
I have an input box like so: <input class="textbox" id="query" type="text" name="querybox" />. In jQuery i’m trying to do $('#query').val() and it’s not able to get the value. I realized later that it’s not even getting the textbox! Although I was following the docs here:
http://api.jquery.com/?s=input
Is there something I’m doing incorrectly?
I have the input box in body > wrapper(div) > content(div).
EDIT
I took away all of the irrelevant pieces to my code from this:
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<script type="text/javascript">
var a1 = $("#query").val();
alert('hi');
</script>
<link rel="stylesheet" media="screen" href="/static/stylesheets/main.css">
<link rel="stylesheet" media="screen" href="/static/stylesheets/uni_login.css">
<link rel="stylesheet" media="screen" href="/static/stylesheets/navigation.css">
<script type="text/javascript" src="/static/javascripts/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="/static/javascripts/jquery.autocomplete-min.js"></script>
</head>
<body>
<div id="wrapper">
<div id="content">
<input class="textbox" id="query" type="text" name="querybox" />
</div>
</div>
</body>
</html>
When I take away var a1 = $("#query").val();, I’m seeing the alert. Which tells me there is something wrong with the jquery i’m running…
Try using your javascript inside document ready. You also need to load the jquery library before your own jquery scripts.
HTML
i also took the liberty to actually put something inside value attribute and display it in the alert.
simple jquery example