Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change value of textfield of HTML using javascript

<head>
<script type="javascript">
function display()
    {
        document.getElementById("textField1").value = "abc";
    }
</script>
</head>
<body>

<form id="form1" action="http://google.com">

<input id="textField1" type="text" value="0" align="right" size="13"/><br>

<input id="button1" type="button" value="1" onclick="display()">
</form>
</body>

but the value of textfield is not changing.

Any Ideas what am i doing wrong ??

like image 989
Bugs Happen Avatar asked Oct 31 '12 07:10

Bugs Happen


People also ask

How do I change the value of a text field in HTML?

Change the Input Value Using the setAttribute() Function in JavaScript. We can also use the setAttribute() function instead of the value property to set the input value. We can also use the forms() function instead of the getElementById() or querySelector() function to get the element using the form name and input name ...

How can add value in textbox using JavaScript?

document. getElementById('txtSearch'). value = "Set Value To textbox using javascript"; You can also use Jquery to set textbox value.

How do you change a value in JavaScript?

In order to change an element, you use its argument name for the value you wish to change. For example, let's say we have a button, and we wish to change its value. <input type="button" id="myButton" value="I'm a button!">

How do you set input value?

Sometimes we need to set a default value of the <input> element, This example explains methods to do so. This property set/return the value of value attribute of a text field. The value property contains the default value, the value a user types or a value set by a script.


5 Answers

Your line

document.getElementById("textField1").value = "abc";

is correct, try

    <head>
<script>
function display() {
        document.getElementById("textField1").value = "abc";
    }
</script>
</head>
<body>

<form id="form1" action="http://google.com">

<input id="textField1" type="text" size="13" value="clear" /><br>

<input type="button" onclick="display()">
</form>
</body>
like image 120
MyMomSaysIamSpecial Avatar answered Oct 04 '22 13:10

MyMomSaysIamSpecial


In case the text field is not having id attribute and having name attribute then,

use

document.getElementsByName("name")[0].value="ert";

getElementsByName() returns an array of objects with that specific name,that's why we are using the index 0.

like image 24
DAVIS BENNY 15MIS0426 Avatar answered Oct 04 '22 14:10

DAVIS BENNY 15MIS0426


try

<script type="text/javascript">

instead of

<script type="javascript">

. I believe the latter is not a valid syntax.

Removing the type attribute entirely works as well:

<script>
like image 35
John Dvorak Avatar answered Oct 04 '22 12:10

John Dvorak


Remove the type from your script tag. It's incorrect and making the browser not treat the script contents as JavaScript. Here it is not working, and here it is working (with the type="javascript" removed).

like image 25
T.J. Crowder Avatar answered Oct 04 '22 14:10

T.J. Crowder


It has to be

<script type="text/javascript">
function display()
    {
        document.getElementById("textField1").value = "abc";
    }
</script>

and not

<script type="javascript">
function display()
    {
        document.getElementById("textField1").value = "abc";
    }
</script>
like image 33
defau1t Avatar answered Oct 04 '22 12:10

defau1t