Form names and ids may not start with a digit.
Try this:-
Code:
<form name = "myform">
<input name="Field1" type="text" /><br>
<input name="Field2" type="text" /><br>
<input name="Field3" type="text" /><br>
<input name="Field4" type="text" /><br>
<input name="Field5" type="text" /><br>
<input type = button value = "Change Background Color" onclick = "chk()">
</form>
<script type = "text/javascript">
function chk() {
for(var i=1; i<=5; i++){
document["myform"]["Field" + i].style.backgroundColor = '#FF0000';
}
}
</script>
or if you want to use ids:-
Code:
<script type = "text/javascript">
function chk() {
for(var i=1; i<=5; i++){
document.getElementById("Field" + i).style.backgroundColor = '#FF0000';
}
}
</script>
Quote:
Originally Posted by altergothen
The error I get is y is null???
If I hard code the id's like this...
var y = document.getElementById('2');
it works fine??
|
Yes it does, because '2' is a string value, not a number. But very srongly advised to avoid.
Nothing's black or white in our country - you're either brilliant or you're hopeless. - Commentator, Radio 5 Live