Reputation: 95
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
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