Solved

How to get text from a text input in jquery

Posted on 2010-09-04
6
331 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Select2 jquery help 9 46
gif overlay 3 32
Trying to convert the select box into input field 8 0
Need help with jQuery plugin 10 0
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
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)

759 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

21 Experts available now in Live!

Get 1:1 Help Now