Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 360
  • Last Modified:

How to get text from a text input in jquery

Ok, I admit - I'm a total newbie at jquery (though highly skilled in both C# and Flash ActionScript 3).  I'm missing something obvious.  Every website I look at says, to get the text from a text input, use val().  They even have working examples.  A typical good website with the right answer is http://www.electrictoolbox.com/jquery-get-set-form-values/demos/

It's very clear, and from the way I read it, this html should work fine.

 
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	<title>Test html</title>
	<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
	<script type="text/javascript">
		function searchClicked()
		{
			alert('test input = ' + $('testinput').val() );
		};
				
		function documentReady()
		{
			$('#my_test').click( searchClicked );
		};
				
		$(document).ready( documentReady );
	</script>        
</head>

<body>
<div id="leftPane" class="ui-widget-content">
	<div id="leftPanel">
		<div>
			<form action="">
				Test String: <input type="text" id="testinput" /><br/> 
				<br/>
				<button id="my_test">Test</button>
			</form>
		</div>
	</div>
</div>

</body>
</html>

Open in new window


It doesn't.  When I enter text in the input field and click the button, the result is always undefined.

What am I doing wrong?

Thanks!

(Also attached as a file for easy testing.)
test.html
0
Carnou
Asked:
Carnou
  • 2
  • 2
  • 2
2 Solutions
 
leakim971PluritechnicianCommented:
Missing # : alert('test input = ' + $('#testinput').val() );
0
 
kadabaCommented:
alert('test input = ' + $('#testinput').val() );

you missed the # id selector
0
 
CarnouAuthor Commented:
Wow.  I banged my head on that for an hour.
I'm going to assume you both posted the answer at the same time, and that kadaba was just slightly slower on the draw.  I hope leakim971 won't mind if I split the points.
Thanks for the super-fast answer!
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
CarnouAuthor Commented:
Thanks to both of you!
0
 
leakim971PluritechnicianCommented:
You're welcome! Thanks for the points!
0
 
kadabaCommented:
well you could have given leakim the points, I would not have minded.
except I hope you know # is an id selector, . is a class selector and so on. Information is more important than findings.

Thanks,
Have a nice day ahead.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now