Intelly XAD Posted October 18, 2006 Share Posted October 18, 2006 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 Link to comment https://forums.phpfreaks.com/topic/24313-inserting-the-id-to-read/ Share on other sites More sharing options...
KevinM1 Posted October 18, 2006 Share Posted October 18, 2006 [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. :) Quote Link to comment https://forums.phpfreaks.com/topic/24313-inserting-the-id-to-read/#findComment-110593 Share on other sites More sharing options...
fenway Posted October 19, 2006 Share Posted October 19, 2006 And never use eval() for composing DOM element names! It's a very bad habit, and incredibly slow. Quote Link to comment https://forums.phpfreaks.com/topic/24313-inserting-the-id-to-read/#findComment-111397 Share on other sites More sharing options...
Intelly XAD Posted October 20, 2006 Author Share Posted October 20, 2006 Yes, but that is my problem,,, what is an other way Quote Link to comment https://forums.phpfreaks.com/topic/24313-inserting-the-id-to-read/#findComment-111827 Share on other sites More sharing options...
obsidian Posted October 20, 2006 Share Posted October 20, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/24313-inserting-the-id-to-read/#findComment-111834 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.