Adding images to pause and play instead of color

I don’t understand what I’m doing wrong.

I’m trying to include an image on pause and play. This is of it working without images. I can’t seem to get it to work adding images to pause and play. This is how it works without images, after I include images it doesn’t work.

I’m adding them like this, but it’s not working.

Play: = 'url('’)';

Pause: = 'url('’)';

Red box: you click

Then you get this image:

you click and it plays

pause: you click again and you get this image

It’s supposed to go from the red to the image, you click that, it plays, you click again and it goes to the other image.

1 image is play, another image is pause.

<div style="width:0;" onclick="myObject=document.getElementById('myObj2');'block';'none'">

<div style="width: 50px; height:50px;  cursor: pointer; background-color:#cc333f;">


<div id="myObj2" style="display: none;">

<button id="playButton2" style="display:block; width:50px; height:50px; border:none; cursor: pointer; background-color:#000000;" onclick="  
var player = document.getElementById('player2').volume='1.0';
var button = document.getElementById('playButton2');
  var player = document.getElementById('player2');
  if (player.paused) { = '#000000'; = 'url('')';;
  } else { = 'url('')';

<audio id="player2" preload="none" style="display:none;">
<source src=';' type='audio/mpeg'/>

As a wild guess, it feels like you’d be better off using the JS to change the class on the element, and then reference each of the classes in your CSS, which will have the reference to the appropriate background image in it.

That’s a good idea. The only trouble is that his code is getting far too complex to understand easily, so people will be less likely to want to take the time to try and understand what’s going on there.


How come I’m able to change colors doing it this way, but images it’s giving me an issue?

I’ve only had a short look at your code, but one obvious error is that you’re not properly opening and closing quotes in statements like this = 'url('')';

This happens because of inline JS as you can’t use e.g. double quotes for the surrounding quotes. Of course, as @chrisofarabia said, even better would be to apply classes. Here’s a basic example:


#playButton {
  width: 50px; 
  cursor: pointer; 
  background-image: url('');
} {
  background-image: url('');


<button id="playButton"></button>
<audio id="player" preload="none" style="display:none;">
<source src=';' type='audio/mpeg'/>


var player = document.getElementById('player');

document.getElementById('playButton').addEventListener('click', function() {
  if (this.className === 'active') {
    this.className = '';
  } else {
    this.className = 'active';;

Here’s a fiddle.


Perhaps you’re beginning to understand why messy code is not such a good idea…


Because you’re using inline styles and JavaScript and this is immediately getting far too confusing to debug. I think the quotes I mentioned above are the main issue here, an error directly caused by inline JS.

x-post :-)


Can you show us the code that works before the images were added?

Yes: Here it is.

It goes from red, to black, to border, then back to red.

It starts off red, doing nothing else.
When the red square is clicked, it goes black and nothing happens.
Then then when the black square is clicked it gets a blue border and plays.

That doesn’t seem like it’s supposed to work that way. What do the colors mean? Are you sure it’s supposed to behave that way?

It is, That’s how I have it set up to do.


them image shows
you click that, it plays
you click again, pause.

This better explains it:

Let the horror show continue. This update adds those images you were wanting.


Thank you.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.