I have a javascript function with which I pass a parameter. The parameter represents the identifier of the element (hidden field) on my web page. I want to change the value of this element.
function myFunc(variable){ var s= document.getElementById(variable); s.value = 'New value' }
When I do this, I get an error that the value could not be set because the object is null. But I know that the object is not null, because I see it in the HTML code generated by the browser. Anyway, I tried the following code for debugging
function myFunc(variable){ var x = variable; var y = 'This-is-the-real-id' alert(x + ', ' + y) var s= document.getElementById(x); s.value = 'New value' }
When a warning message appears, both options are the same, but I still get the error message. But everything works fine when I do
var s= document.getElementById('This-is-the-real-id'); s.value = 'New value'
How can I fix this please
EDIT
The element for which I set the value is a hidden field, and the identifier is determined dynamically when the page loads. I tried adding this to the $ (document) .ready function, but didn't work
javascript default-value
jpo Feb 01 '13 at 15:07 2013-02-01 15:07
source share