Solved

How to get text from a text input in jquery

Posted on 2010-09-04
6
332 Views
Last Modified: 2012-05-10
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
Comment
Question by:Carnou
  • 2
  • 2
  • 2
6 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 300 total points
ID: 33603752
Missing # : alert('test input = ' + $('#testinput').val() );
0
 
LVL 22

Assisted Solution

by:kadaba
kadaba earned 200 total points
ID: 33603758
alert('test input = ' + $('#testinput').val() );

you missed the # id selector
0
 
LVL 10

Author Comment

by:Carnou
ID: 33603766
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 10

Author Closing Comment

by:Carnou
ID: 33603768
Thanks to both of you!
0
 
LVL 82

Expert Comment

by:leakim971
ID: 33603776
You're welcome! Thanks for the points!
0
 
LVL 22

Expert Comment

by:kadaba
ID: 33603778
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now