My problem is: (only as example, must not make sense overall):
// make a function and pass part of the statement as argument
function ExampleFunction( argument ) {
document.getElementById('TestID')[0].style.argument = '#f00';
}
// then later onload
ExampleFunction( background );
I found out that it doesn't work this way, but I can't find out how it would be right. If someone could correct the example to send me on my way I would be very happy and thankful.
Hugo Dozois
8,49812 gold badges56 silver badges59 bronze badges
-
possible duplicate: stackoverflow.com/questions/16413167/…Qantas 94 Heavy– Qantas 94 Heavy2013年05月13日 00:10:16 +00:00Commented May 13, 2013 at 0:10
2 Answers 2
Firstly document.getElementById returns a single element (or null if no element is found), so not [0]. secondly if you want to reference a property dynamically use [] notation
// make a function and pass part of the statement as argument
function ExampleFunction( argument ) {
document.getElementById('TestID').style[argument] = '#f00';
}
// then later onload
ExampleFunction( 'background' );
answered May 13, 2013 at 0:11
Musa
97.9k17 gold badges123 silver badges144 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Simon
ps i forgot the [0] from a different function i used to copy where it wasnt id´s. but thankyou anyway for correcting that too :)
getElementById returns a single element and not a collection.
the correct code is:
document.getElementById('TestID').style.background = '#f00';
answered May 13, 2013 at 0:11
monokh
1,9903 gold badges22 silver badges32 bronze badges
Comments
lang-js