I want to add css (margin-top) to an element on woocommerce checkout page that contains the card error messages.
This is the code:
const cardError = document.getElementsByClassName('sumo-pp-stripe-card-errors');
cardError[0].style.marginTop = '100px';
console.log(cardError[0]);
It works fine when I look at it in the console tab.
But in the real html structure I can not see any changes.
What have I done wrong?
3
Answers
To add to html
First make a css class
you can change your desire css style inside here
then write js like this
Well this works Check!
You need to make sure that your JavaScript runs after the DOM contains the elements.
If you are adding the JavaScript code to the page itself, insert your
<script>
element at the end of the<body>
element.If you are adding your code to an existing script that is at the start of the document, put your code in a function, then call that function when the DOM has loaded. Like this:
document.addEventListener("load", myFunction)