0

I'm looking to translate this code into pure javascript so I don't have to use jquery:

$('#msg').show(0).delay(5000).hide(0);

What would be the javascript equivalent?

0

2 Answers 2

5

You can use the following code:

document.getElementById("msg").style.display = 'block';
setTimeout(function () {
  document.getElementById("msg").style.display = 'none';
}, 5000);
#msg {background: #f90; width: 50px; height: 50px;}
<div id="msg">
  Hello
</div>

I have given the CSS for demo purposes to be clear.

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

Comments

-1

An other version of Praveen code using opacity.

var p = document.getElementById('msg');

p.style.opacity = 1;

setTimeout(function() {
 p.style.opacity = 0;
}, 5000)

4 Comments

jQuery hide is roughly equivalent to calling .css( "display", "none" ).
In many case it will work just the same and in other case it might work while display none wouldn't. So still a valid option. No reason to down vote this
In many case yes. But display is the best option to use, it has been made for this purpose
Opacity is the better form of visibility. 1. Opacity has less browser support than display. 2. Opacity doesn't remove the element from the view.

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.