0

I am making a homepage with some animation. I have display: none in a div. I want display: block after some seconds. I am using some Javscript. The problem now is, that I can't search for a timer script.

I want that my CSS wil change after some seconds. My code:

var myVar = setInterval(myTimer, 1000);
    function myTimer() {
        document.getElementsByClassName("logindiv").style.display = "block";
    }

So, how can I change my CSS with a timer?

6
  • You want to change the styles only once? Commented Dec 31, 2017 at 16:17
  • Possible duplicate of Change an element's class with JavaScript Commented Dec 31, 2017 at 16:18
  • Yes @MarouenMhiri Commented Dec 31, 2017 at 16:18
  • @ryanwebjackson its not a duplicate of stackoverflow.com/questions/195951/… Commented Dec 31, 2017 at 16:26
  • Do you need the value of display to always change at certain interval or you need to change the value to change after a certain interval only once? For the latter, consider using setTimeout and not setInterval Commented Dec 31, 2017 at 16:34

2 Answers 2

1

getElementsByClassName return collections. You have use index like the following:

var myVar = setInterval(myTimer, 1000);
function myTimer() {
    document.getElementsByClassName("logindiv")[0].style.display = "block";
    // for test
    document.getElementsByClassName("logindiv")[0].style.color = "red";
}
.logindiv {
  display: none;
}
<div class="logindiv">Test</div>

Sign up to request clarification or add additional context in comments.

Comments

0

document.getElementsByClassName returns all our class elements, You need use a for loop to change for all of them. If you need only one then use elements[0].style.display = "block";

<div class="logindiv" style="display:none">
  This is login div
</div>

<script>
   setTimeout(myTimer, 1000);
    function myTimer() {
        var elements = document.getElementsByClassName("logindiv");
        for(i=0;i<elements.length;i++){
        	elements[i].style.display = "block";
        }
    }
</script>

jsfiddle :: https://jsfiddle.net/5bxmcf8z/

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.