Jump to content

Archived

This topic is now archived and is closed to further replies.

Intelly XAD

Inserting the ID to read

Recommended Posts

Hi, I have a script that changes an element according to the id that is putted into the function, I am doing this with 'eval', as an example:

function changecolor(id) {
  id = eval("divitem_" + id);
  id.style.bgColor = '#000000';
}

this works fine though, but I was wondering if it could be done on the W3C standart, I tried this but it doesn't work:

document.getElementById('divitem_' + id).style.bgColor = '#000000';

Many Thnx in advance  :D

Share this post


Link to post
Share on other sites
[quote author=Intelly XAD link=topic=111877.msg453681#msg453681 date=1161167636]
Hi, I have a script that changes an element according to the id that is putted into the function, I am doing this with 'eval', as an example:

function changecolor(id) {
   id = eval("divitem_" + id);
   id.style.bgColor = '#000000';
}

this works fine though, but I was wondering if it could be done on the W3C standart, I tried this but it doesn't work:

document.getElementById('divitem_' + id).style.bgColor = '#000000';

Many Thnx in advance  :D

[/quote]

That's because bgColor isn't what JavaScript uses to set an element's background-color property.  Try backgroundColor instead. :)

Share this post


Link to post
Share on other sites
And never use eval() for composing DOM element names!  It's a very bad habit, and incredibly slow.

Share this post


Link to post
Share on other sites
well, if you're passing the id to the function, just treat it as a string:
[code]
function changecolor(id) {
  x = document.getElementById("divitem_" + id);
  x.style.background-color = "#000000";
}
[/code]

hope this helps

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.