0

I am trying to cause a specific .div to change background colors when clicked on. As the user will be able to change their colors, the color must be a variable and should be ran when .ready().

I am using a CSS variable '--main-color' as the variable I would like to change the background color to, however I cannot seem to find a jQuery way of solving it while using '$(this)'.

Is there a specific way to apply a custom CSS property/variable with a .click() function?

$(document).ready(function(){

	// CREATE GRID
	for (i = 0; i<1152; i++) {
		var board = document.createElement('div');
		board.className = 'grid';
		board.addId = 'color'
		document.getElementById('container').appendChild(board);
	};
	// END CREATE GRID

  	// HOVER AND CLICK FUNCTION //
  	    $('#color-picker').colorpicker().on('changeColor', function(ev){
    var choice = ev.color.toHex();
        document.body.style.setProperty('--main-color', choice);
	});

  	$('.grid').hover(function(){
        $(this).click(function(){
        	$(this).css('background-color', 'var(--main-color)');
		});
	});
	// END HOVER AND CLICK FUNCTION


});
body {
	background-color: #222;
	margin: 0;
	padding: 0;
	box-sizing: border-box;
	font-family:'Patua One', cursive;
	color: #FFF;
	--main-color: #000;
}
*{
	background-color: #222;
	margin: 0;
	padding: 0;
	box-sizing: border-box;
	font-family:'Patua One', cursive;
	color: #FFF;
}

#title {
	font-family:'Revalia', cursive;
	font-size: 3em;
	color: #FFF;
	text-align: center;
}

.menu {
	float:left;
    clear: left;
    width: 300px;
    margin-top: 5%;
    margin-left:5%;
}

#container {
	height: 800px;
	width: 800px;
	border: 1px solid #424242;
	margin-left: 30%;
	margin-top: 5%;
	background-color: #FFF;
}

.grid {
	height: 20px;
	width: 20px;
	border: 1px dotted #424242;
	background-color: #FFF;
	display: inline-grid;
}

div.grid:hover{;
	background-color: var(--main-color);
}

.active {
	background-color: var(--main-color);
}

.form-control {
    display: block;
    width: 100%;
    height: 34px;
    padding: 6px 12px;
    font-size: 14px;
    line-height: 1.42857143;
    color: #FFF;
    background-color: #222; 
    background-image: none;
    border: 1px solid #F85658;
    border-radius: 4px;
    -webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,.075);
    box-shadow: inset 0 1px 1px rgba(0,0,0,.075);
    -webkit-transition: border-color ease-in-out .15s,-webkit-box-shadow ease-in-out .15s;
    -o-transition: border-color ease-in-out .15s,box-shadow ease-in-out .15s;
    transition: border-color ease-in-out .15s,box-shadow ease-in-out .15s;
}
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>jQuery Sketch</title>

		<!--Javascript & jQuery Imports /-->
		<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha256-k2WSCIexGzOj3Euiig+TlR8gA0EmPjuc79OEeY5L45g=" crossorigin="anonymous"></script>
		<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=" crossorigin="anonymous"></script>
		<script src='js/main.js'></script>
		<!-- Latest compiled and minified CSS -->
		<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
		<!-- Latest compiled and minified JavaScript -->
		<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

		<!--CSS Imports /-->
		<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
		<link href="CSS/style.css" rel="stylesheet" type="text/css">
		<!--Font Imports /-->
		<link href="https://fonts.googleapis.com/css?family=Patua+One|Revalia" rel="stylesheet">

				<!--BOOTSTRAP IMPORTS /-->
		<script src='js/bootstrap-colorpicker.min.js'></script>
		<link rel='stylesheet' href='CSS/bootstrap-colorpicker.min.css'></link>
		<link rel='stylesheet' href='CSS/bootstrap-colorpicker.min.css.map'></link>

	</head>
	<body>
	<div id = 'title'>
	ZACH<span style='color: #F85658'>'</span>S PIXELMAKER</div>
	</div>

	<div class = 'menu'>
		<div class = 'color-section'>
		<p class="ui-corner-all" style="padding:12px;"> Color:
		</p>
		</div>
		<div id="color-picker" class="input-group colorpicker-component">
    <input type="text" value="#000000" class="form-control" />
    <span class="input-group-addon"><i></i></span>
    <script>

    </script>
</div>

	</div>

	<div id = 'container'>
	</div>

	</body>
</html>

3
  • "I am trying to cause a specific .div to change background colors when clicked on." Well then use the right tool for the job: look at the complications this is instead of a simple jquery .click() or .mousedown() and .css() Commented Jul 9, 2017 at 19:47
  • Just store the color's hex code in a JS variable called TheColor and write .css('background', TheColor) where needed Commented Jul 9, 2017 at 20:00
  • 1
    @frenchie Thanks. your solution worked perfectly. I tend to forget the KISS solutions. Commented Jul 9, 2017 at 20:13

1 Answer 1

1

Assign the color to a class, and toggle the class.

document.querySelector('div').addEventListener('click',function() {
  this.classList.toggle('color');
})
body {
  --main-color: #000;
}
.color {
  background-color: var(--main-color);
  color: white;
}
<div>div</div>

Or using jquery...

$(function() {
  $('div').on('click',function() {
    $(this).toggleClass('color');
  });
});
body {
  --main-color: #000;
}
.color {
  background-color: var(--main-color);
  color: white;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div>div</div>

Sign up to request clarification or add additional context in comments.

3 Comments

The issue with toggling a class is that once the color of the class changes, all div's in that class will also change colors. I would like to keep them their originally assigned color when first clicked on.
@ZMonk Sorry, I don't understand and your demo doesn't work. "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. See: How to create a minimal reproducible example."
I am using an offline plugin and can't find a CDN. To clarify: I have tried the toggleClass solution. The problem is that if I reassign the background-color in the .color class, then all div's in that class will update their colors to the new assigned background color. Think of a box with 4 squares. If I assign the top two to Green, then change the variable to a new color, those top two will change colors instead of staying green because I reassigned the class' color.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.