Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Javascript - Setting <img> background color

img javascript background colour

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1 JakeWelton

JakeWelton

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 152 posts

Posted 31 August 2015 - 11:21 AM

Hi all,

 

I'm trying to dynamically change the background colour of an <img> tag element. The background colour change works fine in css (its used with the :hover property) however for some reason JavaScript wont change it. This is the code i'm trying to use:

document.getElementById('element').style.backgroundColor = 'red';

Does anyone know why this might be?

 

Thanks


sig.jpg

"Before you judge someone, walk a mile in their shoes; after that, who cares?! Their a mile away and you've got their shoes!"


#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts

Posted 31 August 2015 - 11:34 AM

You'll have to show us the rest of the code, as there's nothing that will cause that code to fire, or to fire on the correct item.


Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 JakeWelton

JakeWelton

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 152 posts

Posted 31 August 2015 - 11:46 AM

You'll have to show us the rest of the code, as there's nothing that will cause that code to fire, or to fire on the correct item.

 

Okay... essentially the code is as follows:

<img id="tester" onClick="document.getElementById('tester').style.backgroundColor = 'red'" src="images/myImage.png">

sig.jpg

"Before you judge someone, walk a mile in their shoes; after that, who cares?! Their a mile away and you've got their shoes!"


#4 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts

Posted 01 September 2015 - 01:14 AM

Works fine?

http://jsfiddle.net/64ak4xf5/



#5 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts

Posted 01 September 2015 - 06:51 AM

Also works with "this" instead of "document.getElementById('tester')"


My Blog: http://forum.codecal...699-blog-77241/
"Women and Music: I'm always amazed by other people's choices." - David Lee Roth




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download