3

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?

1 Answer 1

4

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";
Sign up to request clarification or add additional context in comments.

Comments

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.