0

I have an <input type="text" value="0.00" id="prdtotal"/> inside a <td> in a table.

I want to use Javascript to compute a value and assign it to the textbox. How can I do it?

I tried using document.getElementById("prdtotal").value="1.00"; but it doesnt work. Thanks for any help!

2
  • it seems like document.getElementById cannot find the <inpt> because it is inside a <td> Commented Jul 31, 2013 at 15:45
  • 1
    document.getElementById will find any element by id no matter how deeply nested inside other elements it is. Commented Jul 31, 2013 at 15:48

2 Answers 2

2

Use the value attribute:

document.getElementById("prdtotal").value = "1.00";

document.getElementById() returns the actual DOM object (a handle/reference to the <input> element).

Also note your missing double quote before the closing parenthesis.

I would suggest you to take a look at the Mozilla Developer Network (MDN):

  • HTMLElement: a reference of all attributes a (general) HTML element can have.

  • HTMLInputElement: a reference of all special attributes an input element (e.g. <textarea>, <input>) can have.

Sign up to request clarification or add additional context in comments.

5 Comments

Why does it not seem to woork for me?
@Umpong_1947676 What does not work for you? Which code did you use?
I am triggering a change in the value of the textbox from a javascript called by a change in the value of another textbox. I tried the value to be passed on the second textbox(supposed to contain the answer) through an alert and it shows... however, whenever I pass the value to the text box using document.getElementById("prdtotal").value="2.00"; the value of the textbox does not change
@Umpong_1947676 Please post another question for that.
2

You've found the element using getElementById but you've not specified its the value property you want to set.

document.getElementById("prdtotal").value="1.00"

And a live example: http://jsfiddle.net/hevwt/

1 Comment

Oh, I just missed it in the post but i have it on my code... although it still wont work... dont know what the problem is....

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.