Jump to content

[SOLVED] Javascript


jwwceo

Recommended Posts

Hello,

 

I am having trouble with using Javascript to change the background color of a TD. This is supposed to happen when a user changes a pulldown...

 

here is the relevant javascript code:

 

window.document.jwv.bgColor = "#f3f3f3";

 

and the HTML

 

<td name="jwv" valign="bottom" width="200" height="162">
<img id="product_thumbnail">
</td>

 

I get an error saying that jwv has no properties.

 

The TD is rendered before the Javascript runs.

 

Any ideas??

 

James

Link to comment
Share on other sites

You would be much better off using id attributes since name is not a valid TD attribute. Try this instead:

 

HTML:

<td id="jwv" valign="bottom" width="200" height="162">
<img id="product_thumbnail" />
</td>

 

Javascript:

document.getElementById('jwv').style.backgroundColor = '#f3f3f3';

 

If you have multiple cells that you are wanting to change, you'll want to assign them all a class attribute instead of id, and then you just loop over them like this:

eles = document.getElementsByTagname('TD');
for (var i = 0; i < eles.length; i++)
{
  if (eles[i].className == 'myclassname')
  {
    eles[i].style.backgroundColor = '#f3f3f3';
  }
}

 

Hope this helps.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.