12
<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 ??

1
  • i want it to change from "0" to "abc" well actually i am creating a calculator and i will pass "1" but in near future, when this issue is resolved Commented Oct 31, 2012 at 7:51

7 Answers 7

8

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>
Sign up to request clarification or add additional context in comments.

4 Comments

Better to remove it entirely.
I was gonna say, append this to mention that you can just do <script> and most modern web browsers default to javascript
@T.J.Crowder it works but is it clean? Stack Overflow includes the type attribute.
@JanDvorak: Yes. The default for type is and always has been JavaScript. Code Bundle says "most modern web browsers" but it's not most, it's all, modern right the way through to "ancient" from the mid 90's.
4

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>

Comments

3

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).

Comments

3

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>

Comments

1

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.

Comments

0

There is nothing wrong with your code, it works fine in this fiddle.

I only speculate, but you could try to either delete the type attribute of the script-tag, or change it to type="text/javascript" which would be the proper type to use. If you don't specify it, the browser will consider it as JavaScript by default.

Comments

0

It's actually correct, but if it doesn't work try like this:

document.getElementById("textfield1").innerHtml = "ABC";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.