Jump to content

Javascrict, OnChange event alter CSS element?


spikypunker

Recommended Posts

Hi dudes, i've implemeted a javascript tool that returns a HEX value from a color picker, it currently adds the value into an Input box. I really want to have the HEX value also get fired into a CSS rule?

 

Here's the code snippet that i've amended, it contains the event and the previous action which added the HEX to the input box, and also the line ive TRIED to add which i thought would change the CSS rule but yeah it's not working

 

Any ideas?? Cheers guys!!

 

 


'onChange': function(color) {
$('myInput').value = color.hex;
document.getElementById(bgcolor1).style.backgroundColor = color.hex;

 

Bear in mind this is part of a slightly larger function which i can paste too, but this is the snippet i wanted to add the change to!

 

Peace, Chris

Edit/Delete Message

Link to comment
Share on other sites

I've solved this on me own guys, thanks the the help

 

Here's the code if anyones interested! Its part of the code from the tool MooRainbow, and what i've added changes a CSS class which then changes the background color of table cells with that class, in realtime. Pretty cool if i do say so myself.

 

<script type="text/javascript">
window.addEvent('domready', function() {
var r = new MooRainbow('myRainbow', {
	'startColor': [58, 142, 246],
	'onChange': function(color) {
		$('myInput').value = color.hex;


						var css = document.styleSheets[0];
							try { // IE
    									css.addRule('.bgcolor1', 'background-color:' + color.hex + ';');
    								}
						catch(e) { // W3C
    							css.insertRule('.bgcolor1 {background-color:' + color.hex + ';}',css.cssRules.length);
   								 }

	}
});
});
</script>

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.