03-26-2012, 03:45 PM
I have this code but it doesnt work:
var number = 10;
document.getelementbyid(number).value = number;
<input type="text" id="number" value="">
Can anybody help me out here?
03-26-2012, 04:32 PM
A lot of errors in so few lines!
c) The id of the textbox is a literal so must be in quotes. Without the quotes the reference is to the variable number, which has the value of 10, which is nonsense. (Just in case - an id must start with a letter).
d) Your script attempts to refer to the textbox before it exists.
<input type="text" id="mynumber" value="">
var number = 10;
document.getElementById("mynumber").value = number;
Quizmaster: In astronomy, a nucleus, a coma and a tail are parts of which celestial body?
Contestant: A horse.
03-26-2012, 04:36 PM
Thanks works perfectly.
Sorry about the errors lol.
03-26-2012, 04:47 PM
There is an error in your syntax . Capital letters are important.
var number1 = 10;
var number = document.getElementById("number");
// number.value = "Hello World"; // <-- This does not work jet.
<input type="text" id="number">
But I could not assign the value jet.
03-26-2012, 05:19 PM
Note that in Internet Explorer, names and id's are global variables and thus you should NEVER use a (global) variable or function name which is the same as an HTML element name or id. Example:-
<input id = "text">
text = "Hello World"; // global variable
document.getElementById('text').value = text;
I agree that the variable here is local because it is declared with the var keyword. But you are playing with fire! Follow my simple rule.
Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.