RussellHarrower
RussellHarrower

Reputation: 6810

jQuery - Make input value a var

I want to put a var text = "hello"

Into an input value. I am not sure how to do this, and I would like to know to this, as I am not sure.

I thought It would of just been $(input#text).text(text); but that did not work

Upvotes: 1

Views: 5021

Answers (4)

Amritpal Singh
Amritpal Singh

Reputation: 1785

you can use

var text = "hello"
$('#id').val(text);
  //where id is the id assigned to your text box
 //eg.
<input type='text' id='mytextbox' value=''/>
 //it should be then 
var text = "hello"
$('#mytextbox').val(text);

Upvotes: 1

Sandeep Pathak
Sandeep Pathak

Reputation: 10747

To assign value of a text box whose id is textbox in jQuery ,do the following :-

$("#textbox").val(text);

See val

Upvotes: 1

John Leidegren
John Leidegren

Reputation: 60997

If you look up the documentation of jQuery for text, you'll see that text() manipulates text nodes (or text node content). If you're setting a value on a textbox you should be using the val() function.

$("input#text")
    .val(text); 

Upvotes: 2

Sarfraz
Sarfraz

Reputation: 382726

Use val() not text():

$("#text").val(text);

The val() is used when you want to read from or write to text fields.

Upvotes: 1

Related Questions