# Issues making buttons work with JS

**URL:** <https://forum.freecodecamp.org/t/issues-making-buttons-work-with-js/408957>\
**Category:** General\
**Created:** [July 10, 2020, 7:44pm UTC](https://forum.freecodecamp.org/t/issues-making-buttons-work-with-js/408957 "2020-07-10T19:44:02Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![spennington](https://avatars.discourse-cdn.com/v4/letter/s/f1d935/32.png) [@spennington](https://forum.freecodecamp.org/u/spennington)\
**Post date:** [July 10, 2020, 7:44pm UTC](https://forum.freecodecamp.org/t/issues-making-buttons-work-with-js/408957/1 "2020-07-10T19:44:02Z")

</div>

**Tell us what’s happening:**  
I can not make a HTML button do anything . I am trying to make it change the background color of a div element and I am failing at this for longer then I wish to admit. The important bit for everything down is a little bit of html and the js. I also included the css for reference just in case it’s needed.

**Your code so far**  
$(document).ready(function(){

```
var percentChance = 50;
var sp = "";
var tl = 0;
var trial = 1;
var dish1count = [];
var dish2count = [];
var report = [];

$('#button_one').on({'click': function(){
 		
 	// change color
	document.documentElement.style.cssText = "--color: green");
		
});

```

});

```auto

<body>

	<h1>Scientific method</h1>	
	<div class="grid-container0">
		<p>Set parameters for each dish.</p>
	</div>
	<div class="grid-container1">
		<div class="item-1">
  			<button id="button_one" class="button">Humid</button> <br>
  			<button id="button_two" class="button">Dark</button>
		</div>
		<div class="left_circle" id="left_circle">
			<img src="images/pillbug.png" id="pic_two" alt="petri_dish" class="pic_two">
			<img src="images/pillbug.png" id="pic_two" alt="petri_dish" class="pic_two">
			<img src="images/pillbug.png" id="pic_two" alt="petri_dish" class="pic_two">
			<img src="images/pillbug.png" id="pic_two" alt="petri_dish" class="pic_two">
			<h2>4</h2>
		</div>
		<div class="right_circle" id="right_circle">
			<img src="images/pillbug.png" id="pic_two" alt="petri_dish" class="pic_two">
			<img src="images/pillbug.png" id="pic_two" alt="petri_dish" class="pic_two">
			<img src="images/pillbug.png" id="pic_two" alt="petri_dish" class="pic_two">
			<img src="images/pillbug.png" id="pic_two" alt="petri_dish" class="pic_two">
			<h2>4</h2>
		</div>
		<div class="item-4">
			<button id="button_three" class="button">Humid</button> <br>
			<button id="button_four" class="button">Dark</button>
		</div>
	</div>
		<div class="grid-container2">
		<p> Click button to save and continue</p>
		<div class="item-5">
  			<button id="button_five" class="button_five">Add a minute</button>
		</div>
	</div>

</body>
</html>

--CSS
:root {
	--color: white;
}

* {

	font-family: helvetica;
	font-size: 1em;

}

body {
	margin: 30px 0px;
	background-color: #999;
	text-align: center;
}

h1 {
	color: blue;
	text-align: center;
	font-size: 50px;
}

h2 {
	color: yellow;
	alpha: 50%;
	font-size: 50px;
	text-align: center;
	opacity: 0.4;
}

.grid-container0 {
  display: grid;
  grid-template-columns: auto 300px 300px auto;
  background: blue;
  margin: 0% 20%;
  text-align: center
}

.grid-container1 {
  display: grid;
  grid-template-columns: auto 300px 300px auto;
  background: linear-gradient(blue, lightblue);
  margin: 0% 20%;
  text-align: center
}

.grid-container2 {
  display: grid;
  background: lightblue;
  margin: 0% 20%;
}

p {
	text-align: left;
	margin-left: 20px;
}

.left_circle {
  height: 300px;
  width: 300px;
  background-color: var(--color);
  border-radius: 50%;
  border-style: solid;
}

.right_circle {
  height: 300px;
  width: 300px;
  background-color: var(--color);
  border-radius: 50%;
  border-style: solid;
}

.button {
	margin: 20px;
}

.button_five {
	margin: 20px;
	background-color: orange;
	padding: 10px;
}

**Your browser information:**

Google Crome
```

---

<div class="post-metadata">

**Author:** ![bbsmooth](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bbsmooth/32/203556_2.png) [@bbsmooth](https://forum.freecodecamp.org/u/bbsmooth)\
**Post date:** [July 10, 2020, 7:46pm UTC](https://forum.freecodecamp.org/t/issues-making-buttons-work-with-js/408957/2 "2020-07-10T19:46:33Z")

</div>

It looks like you are using jQuery. That’s not quite how you use the `on` function to attach an event handler to a button. See the examples on the documentation:

[https://api.jquery.com/on/](https://api.jquery.com/on/)

---

<div class="post-metadata">

**Author:** ![spennington](https://avatars.discourse-cdn.com/v4/letter/s/f1d935/32.png) [@spennington](https://forum.freecodecamp.org/u/spennington)\
**Post date:** [July 10, 2020, 7:59pm UTC](https://forum.freecodecamp.org/t/issues-making-buttons-work-with-js/408957/3 "2020-07-10T19:59:36Z")

</div>

Yes to jQuery  
I have changed it to look like the sample in the link you provided and added an alert but it still doesn’t seem to be working. I don’t see what I am doing wrong. I have also tried several other ways to make it work but the button never does anything.

```
$("#buttion_one").on( "click", function() {
 		
 	// change color
	document.documentElement.style.setProperty("--color", "green");
	alert( "clicked" );
		
});
```

---

<div class="post-metadata">

**Author:** ![bbsmooth](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bbsmooth/32/203556_2.png) [@bbsmooth](https://forum.freecodecamp.org/u/bbsmooth)\
**Post date:** [July 10, 2020, 8:01pm UTC](https://forum.freecodecamp.org/t/issues-making-buttons-work-with-js/408957/4 "2020-07-10T20:01:50Z")

</div>

I think we will need to a link to your entire code base before we can go any further. Do you have this somewhere we can look at it?

Also, I just noticed that there is a syntax error in the original code you posted:

`document.documentElement.style.cssText = "--color: green");`

Do you see the problem here?

UPDATE: I see you fixed it in the snippet you just posted, so ignore that.

---

<div class="post-metadata">

**Author:** ![spennington](https://avatars.discourse-cdn.com/v4/letter/s/f1d935/32.png) [@spennington](https://forum.freecodecamp.org/u/spennington)\
**Post date:** [July 10, 2020, 8:08pm UTC](https://forum.freecodecamp.org/t/issues-making-buttons-work-with-js/408957/5 "2020-07-10T20:08:57Z")

</div>

It did not fix it and I am putting it on Github now and will share the link in a min. thanks

---

<div class="post-metadata">

**Author:** ![godswill199](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/godswill199/32/166599_2.png) [@godswill199](https://forum.freecodecamp.org/u/godswill199)\
**Post date:** [July 10, 2020, 8:22pm UTC](https://forum.freecodecamp.org/t/issues-making-buttons-work-with-js/408957/6 "2020-07-10T20:22:16Z")

</div>

I suggest you create a function and use the conditional statement inside then you can attach that function to your button. onClick of the button the function fires

---

<div class="post-metadata">

**Author:** ![spennington](https://avatars.discourse-cdn.com/v4/letter/s/f1d935/32.png) [@spennington](https://forum.freecodecamp.org/u/spennington)\
**Post date:** [July 10, 2020, 8:31pm UTC](https://forum.freecodecamp.org/t/issues-making-buttons-work-with-js/408957/7 "2020-07-10T20:31:22Z")

</div>

> **[spennington45/Pill\_bug\_lab](https://github.com/spennington45/Pill_bug_lab)**
>
> Contribute to spennington45/Pill\_bug\_lab development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![bbsmooth](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bbsmooth/32/203556_2.png) [@bbsmooth](https://forum.freecodecamp.org/u/bbsmooth)\
**Post date:** [July 10, 2020, 9:07pm UTC](https://forum.freecodecamp.org/t/issues-making-buttons-work-with-js/408957/8 "2020-07-10T21:07:57Z")

</div>

Two things in button\_script.js:

- You’ve got an errant right curly brace on line 38
- Look closely at the ID you are using to set the click handler on line 25

---

<div class="post-metadata">

**Author:** ![spennington](https://avatars.discourse-cdn.com/v4/letter/s/f1d935/32.png) [@spennington](https://forum.freecodecamp.org/u/spennington)\
**Post date:** [July 11, 2020, 1:41am UTC](https://forum.freecodecamp.org/t/issues-making-buttons-work-with-js/408957/9 "2020-07-11T01:41:19Z")

</div>

Wow bbsmooth the ID fixed it and I feel so dumb. Thanks for the help!!

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 6:47pm UTC](https://forum.freecodecamp.org/t/issues-making-buttons-work-with-js/408957/10 "2021-06-01T18:47:25Z")

</div>

Closed on June 1, 2021, 6:47pm UTC


