I am trying to run a function that changes the margin-top size according to what is being displayed. However, it doesn’t seem to be working?
JavaScript
x
14
14
1
<body onload="changeFooter()">
2
3
<script>
4
5
const heading = document.getElementById('verify');
6
const footer = document.getElementById('footer')
7
8
9
function changeFooter () {
10
if (heading == true){
11
footer.style.marginTop = "200px"
12
}
13
}
14
also tried this
JavaScript
1
13
13
1
function changeFooter () {
2
if (heading.match('Verify')){
3
footer.style.marginTop = "200px"
4
}
5
}
6
7
8
9
</script>
10
11
12
<h1 id="verify" class="verifyheading">Verify Identity</h1>
13
Thank you
Advertisement
Answer
document.getElementById
returns the element (if it exists) or null, not a boolean (true/false).
You can simply do if(heading) { ... }
as your condition.
Here’s a snippet based on your code: https://codepen.io/29b6/pen/XWZVqWx