Avatar of Whing Dela Cruz
Whing Dela CruzFlag for Anguilla

asked on 

Timer on div

Hi experts, would it be possible to put timer on div? I have a div with an id, txtHint. then, I have this command document.getElementById("txtHint").innerHTML="Hello" Here, the message "hello", I want it to disappear after  3 seconds. Thanks!  

<div id="txtHint"></div>
* divHTMLJavaScript

Avatar of undefined
Last Comment
Leonidas Dosas
ASKER CERTIFIED SOLUTION
Avatar of Prakash Samariya
Prakash Samariya
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Whing Dela Cruz

ASKER

Thank you Prakash, now its working!
Try with below code change
<script>
function Go(e)
{
   var msg = "Hello!"
   document.getElementById("txtHint").innerHTML=msg;
   setTimeout(function(){ document.getElementById("txtHint").innerHTML="" }, 3000);
}
</script>

Open in new window

welcome :)
HTML

<button onclick="Go()">Show</button>

<div id="txtHint">MyHint</div>

Open in new window


JS
//Initialy i set var with the div element
var divElm=document.getElementById('txtHint');
//I hide the divElm
divElm.style.display='none';
//I create a function that when it invokes the div logo appear
function Go()
{
  var msg = "Hello!";
  divElm.innerHTML=msg;
  divElm.style.display='inline';
  //I use the setTimeout to hide the divElm after 3 sec=3000ms
  setTimeout(function(){
  divElm.style.display='none';  
  }, 3000);
}

Open in new window

JavaScript
JavaScript

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo