/    Sign up×
Community /Pin to ProfileBookmark

Simple, ..yet doesn’t work

just wondering why my syntax isn’t working here..

this doesn’t work…

[code=php]function setBg() {
el = document.getElementsByTagName(‘body’);
el.style.backgroundColor = “#f9f363”;
}[/code]

but this does…..

[code=php]function setBg() {
document.body.style.backgroundColor = “#f9f363”;
}[/code]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@bionoidAug 23.2012 — getElementsByTagName returns a collection, so this should work:

[CODE]function setBg() {
el = document.getElementsByTagName('body')[COLOR="Red"][0][/COLOR];
el.style.backgroundColor = "#f9f363";
} [/CODE]
Copy linkTweet thisAlerts:
@JMRKERAug 23.2012 — just wondering why my syntax isn't working here..

this doesn't work...
[code=php]function setBg() {
el = document.getElementsByTagName('body');
el.style.backgroundColor = "#f9f363";
}[/code]


but this does.....


[code=php]function setBg() {
document.body.style.backgroundColor = "#f9f363";
}[/code]
[/QUOTE]


getElementsByTagName() returns an array, not an individual value.

Needs to be accessed like an array element.

See:
<i>
</i>&lt;script type="text/javascript"&gt;
function setBG() {
el = document.getElementsByTagName('body');
el[0].style.backgroundColor = "#00f363";
}

function SETbg() {
document.body.style.backgroundColor = "#f9f363";
}
&lt;/script&gt;
&lt;body&gt;
&lt;button onclick="setBG()"&gt;Tag BG set&lt;/button&gt;
&lt;button onclick="SETbg()"&gt;DOM BG set&lt;/button&gt;
&lt;/body&gt;
Copy linkTweet thisAlerts:
@toptomatoauthorAug 23.2012 — ahh yes, got it. thank you!
Copy linkTweet thisAlerts:
@JMRKERAug 23.2012 — You're most welcome.

Happy to help

Good Luck!

?
×

Success!

Help @toptomato spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...