Jump to content


Photo

Inserting the ID to read


  • Please log in to reply
4 replies to this topic

#1 Intelly XAD

Intelly XAD
  • Members
  • PipPip
  • Member
  • 14 posts
  • LocationNetherlands

Posted 18 October 2006 - 10:33 AM

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

Experience does the trick...

An amateur is commonly better than someone who studied for it...

Don't mind my English, trust me, don't...

#2 KevinM1

KevinM1
  • Moderators
  • Snarkimus Prime
  • 5,248 posts

Posted 18 October 2006 - 11:32 AM

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


That's because bgColor isn't what JavaScript uses to set an element's background-color property.  Try backgroundColor instead. :)
Using 'global' is a sign of doing it wrong

#3 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 19 October 2006 - 04:49 PM

And never use eval() for composing DOM element names!  It's a very bad habit, and incredibly slow.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#4 Intelly XAD

Intelly XAD
  • Members
  • PipPip
  • Member
  • 14 posts
  • LocationNetherlands

Posted 20 October 2006 - 01:35 PM

Yes, but that is my problem,,, what is an other way
Experience does the trick...

An amateur is commonly better than someone who studied for it...

Don't mind my English, trust me, don't...

#5 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 20 October 2006 - 01:44 PM

well, if you're passing the id to the function, just treat it as a string:
function changecolor(id) {
  x = document.getElementById("divitem_" + id);
  x.style.background-color = "#000000";
}

hope this helps
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users