Jump to content

Change text colour with button


Nodral

Recommended Posts

Hi

 

I'm creating a control panel which allows the user to input some text and then play around with the colours and fonts whilst seeing it in a preview panel.  My Javascript is not that strong and I'm struggling to get the input color.

 

 

CSS


  #colour1{
  background-color:red;
  }

 

HTML controls

<td class="colours"><button id="colour1" onClick="changeColor('colour1');"></button></td>
<td  rowspan="4" width="60%"><span id="preview" >Hello World</span></td>

 

js Function

 

function changeColor(input){

var element  =document.getElementById(input);

var elementColor  = document.getElementById(input).style.backgroundColor;

document.getElementById('preview').style.color=document.getElementById(input).style.backgroundColor;


}

 

This does absolutely nothing, how can I make it work?

 

Link to comment
https://forums.phpfreaks.com/topic/257187-change-text-colour-with-button/
Share on other sites

You're passing the ID of your button to the function, which then tries to set the new colour as the value property of the object with that ID (your button), which is undefined. In other words you're trying to set the colour to something undefined. Where is the new colour value stored/input?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.