0

I have my markup like this

<div class="text-input-area"> <input type="text" id="text-input"/> <br /> <input id="button" type="submit" value="Preview" /> </div><!--.text-input-area--> 

In the input area I will type only text. Now when I will click on button Preview it should show the text that I had type. For that I have made my jquery like this

<script type="text/javascript"> jQuery(document).ready(function() { jQuery('#button').click(function() { var textvalue = $( "#text-input").text(); alert('textvalue'); }); }); </script> 

But its not working at all. Can someone tell me how to do this?

5 Answers 5

3

demo http://jsfiddle.net/gmeEL/

API:

Hope this will fit the cause :)

code

 jQuery(document).ready(function() { jQuery('#button').click(function() { var textvalue = $( "#text-input").val(); alert(textvalue); }); }); 
Sign up to request clarification or add additional context in comments.

Comments

1

Use $( "#text-input").val() instead of $( "#text-input").text() and alert(textvalue) instead of alert('textvalue')

Complete Code:

jQuery(document).ready(function() { jQuery('#button').click(function() { var textvalue = $( "#text-input").val(); alert(textvalue); }); }); 

Fiddle Demo

Comments

0

Two problems

  1. need to use .val() to get the value of an input field
  2. need to use the variable textvalue for the alert not a string literal 'textvalue'

Try

jQuery(function ($) { $('#button').click(function () { var textvalue = $("#text-input").val(); alert(textvalue); }); }); 

Comments

0

for inputs and select use $(selector).val() for span use $(selector).text()

Comments

0

It should be a val() i.e. a jquery function that returns a value of the element

var textvalue = $( "#text-input").val(); 

Please note that

The .text() method cannot be used on form inputs or scripts. To set or get the text value of input or textarea elements, use the .val() method. To get the value of a script element, use the .html() method.

http://api.jquery.com/text/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.