nesorethan
nesorethan

Reputation: 95

How can my Javascript function change the style of an html button

I'm new to JS

I have a button in my HTML and I want my JavaScript function to change the padding on the button when clicked:

let btn = document.querySelector('.paddingButton');

btn.addEventListener('click', function() {
  btn.style.padding = "'" + Math.random() * 25 + "em" + "'";
});
<button class="paddingButton">Button</button>

This doesn't work. Can somebody tell me why?

Upvotes: 3

Views: 41

Answers (1)

Dennis
Dennis

Reputation: 32598

You have extra quotes here that are needed in CSS, but not JS:

btn.style.padding = "'" + Math.random() * 25 + "em" + "'";

replace it with just the number + "em":

btn.style.padding = Math.random() * 25 + "em";

Upvotes: 4

Related Questions