|
-
Simple, ..yet doesn't work
just wondering why my syntax isn't working here..
this doesn't work...
PHP Code:
function setBg() {
el = document.getElementsByTagName('body');
el.style.backgroundColor = "#f9f363";
}
but this does.....
PHP Code:
function setBg() {
document.body.style.backgroundColor = "#f9f363";
}
-
getElementsByTagName returns a collection, so this should work:
Code:
function setBg() {
el = document.getElementsByTagName('body')[0];
el.style.backgroundColor = "#f9f363";
}
All code given is free and non-refundable.
-
 Originally Posted by toptomato
just wondering why my syntax isn't working here..
this doesn't work...
PHP Code:
function setBg() {
el = document.getElementsByTagName('body');
el.style.backgroundColor = "#f9f363";
}
but this does.....
PHP Code:
function setBg() {
document.body.style.backgroundColor = "#f9f363";
}
getElementsByTagName() returns an array, not an individual value.
Needs to be accessed like an array element.
See:
Code:
<script type="text/javascript">
function setBG() {
el = document.getElementsByTagName('body');
el[0].style.backgroundColor = "#00f363";
}
function SETbg() {
document.body.style.backgroundColor = "#f9f363";
}
</script>
<body>
<button onclick="setBG()">Tag BG set</button>
<button onclick="SETbg()">DOM BG set</button>
</body>
-
ahh yes, got it. thank you!
-
You're most welcome.
Happy to help
Good Luck!
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|
Bookmarks