Nodral Posted February 17, 2012 Share Posted February 17, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/257187-change-text-colour-with-button/ Share on other sites More sharing options...
Adam Posted February 17, 2012 Share Posted February 17, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/257187-change-text-colour-with-button/#findComment-1318348 Share on other sites More sharing options...
nogray Posted February 17, 2012 Share Posted February 17, 2012 You can't access the css properties using obj.style.prop unless it's an inline style. You would need to use currentStyle Quote Link to comment https://forums.phpfreaks.com/topic/257187-change-text-colour-with-button/#findComment-1318457 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.