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
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?

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.