AJM
AJM

Reputation: 32490

Jquery/Javascript Setting the attribute value of a textfield

I have a

<input type='text' id='text' value='' />

How do I programmatically set the value attrribute using JQuery/Javascript

Upvotes: 31

Views: 67454

Answers (4)

Rob Knight
Rob Knight

Reputation: 8772

In jQuery, you'd do it like this:

$("#text").val("my new value");

You might also want to read the jQuery documentation on this topic

Without jQuery:

document.getElementById("text").setAttribute("value", "my new value");

Hope that helps.

Upvotes: 9

karim79
karim79

Reputation: 342635

The Javascript would be:

document.getElementById('text').value = 'Blahblah';

Upvotes: 13

Bojan Rajkovic
Bojan Rajkovic

Reputation: 1706

The simple easy way is:

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

Upvotes: 70

Max Schmeling
Max Schmeling

Reputation: 12509

It's as easy as this:

$("#text").attr("value", "some value");

Upvotes: 22

Related Questions