Tony33
Tony33

Reputation: 147

javascript style display "none" not working

I'm trying to hide or display html codes based on javascript if statement.

It's not working. You can see a fiddle here: https://jsfiddle.net/ajhv0pLj/

<script type="text/javascript">
if (window.localStorage.getItem("deflang") === null) {
    alert (window.localStorage.getItem("deflang"));
    document.getElementById('homepage').style.display = 'none';
}
</script>

<div id="homepage">
Hello world
</div>

Upvotes: 0

Views: 953

Answers (2)

Tony33
Tony33

Reputation: 147

i found the solution, i had to put the script before the body tag close. Or just after my div declaration.

Basically, this worked:

<div id="homepage">
Hello world
</div>

<script type="text/javascript">
if (window.localStorage.getItem("deflang") === null) {
    alert (window.localStorage.getItem("deflang"));
    document.getElementById('homepage').style.display = 'none';
}
</script>

Upvotes: 0

Kevin
Kevin

Reputation: 1367

The code is working, you don't have to put the script tag around your javascript in jsfiddle, that's what's throwing the error.

Upvotes: 1

Related Questions