Helping ordinary people create extraordinary websites!

Go Back   Web Development Forum > Website Programming > Client-Side Scripting
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-26-2008, 06:51 AM
Junior Member
 
Join Date: Dec 2007
Posts: 24
Default why isnt my javascript displaying the text

hi, i have written the following javascript, however when i click the button it wont display the text. can anyone see what the problem is and how to get it working. i am trying to do it so the text is displayed in a sequence.
thanks

<html>
<head>
<script type="text/javascript">
function timedText()
{
var t1=setTimeout("document.getElementById('displayDiv ').value='2 seconds!'",2000);
var t2=setTimeout("document.getElementById('displayDiv ').value='4 seconds!'",4000);
var t3=setTimeout("document.getElementById('displayDiv ').value='6 seconds!'",6000);
}
</script>
</head>
<body>
<form>
<input type="button" value="Display timed text!" onClick="timedText()">
<div id="displayDiv">
</div>
<p>Click on the button above. It will tell you when two, four, and six seconds have passed.</p>
</body>
</html>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 01-27-2008, 04:23 AM
BigAlReturns's Avatar
Moderator Extraordinaire!
 
Join Date: Dec 2007
Location: The Wirral, England
Posts: 298
Send a message via MSN to BigAlReturns
Default

I think the problem is that divs don't have values, they have innerHTML. Try using this instead and it should work, although you may have to play around to get them to not replace each other.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 01-28-2008, 02:35 PM
Junior Member
 
Join Date: Dec 2007
Posts: 24
Default

hi,
thanks your were right i had to change it to your suggestion it works now,
do you know what i can do to make it so that when i click the button it gets rid of the text 'Click on the button above. It will tell you when two, four, and six seconds have passed' and just displays the sequence 2seconds, 4seconds 6 seconds?
hope that makes sense,

thanks

Last edited by skat : 01-28-2008 at 02:38 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 01-28-2008, 03:56 PM
BigAlReturns's Avatar
Moderator Extraordinaire!
 
Join Date: Dec 2007
Location: The Wirral, England
Posts: 298
Send a message via MSN to BigAlReturns
Default

If you stick the <p> containing the instructions inside the displayDiv, then it will be replaced by the 2 seconds, 4 seconds etc text when the button is clicked. I think that's what you mean anyway!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 01-28-2008, 04:08 PM
Junior Member
 
Join Date: Dec 2007
Posts: 24
Default

hi,
i dont really understand where you mean to put that line of text, i have tried placing it in many areas however it still doesnt work. my aim is for the line of text to appear when the page loads and then when the user clicks the button it will disappear and then sequence of 2sec, 4 secs and 6 sec will appear. could you please give me an example?

thank you
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 01-28-2008, 06:06 PM
BigAlReturns's Avatar
Moderator Extraordinaire!
 
Join Date: Dec 2007
Location: The Wirral, England
Posts: 298
Send a message via MSN to BigAlReturns
Default

Code:
<html>
<head>
<script type="text/javascript">
function timedText()
{
document.getElementById('displayDiv').innerHTML='';
var t1=setTimeout("document.getElementById('displayDiv').innerHTML='2 seconds!'",2000);
var t2=setTimeout("document.getElementById('displayDiv').innerHTML='4 seconds!'",4000);
var t3=setTimeout("document.getElementById('displayDiv').innerHTML='6 seconds!'",6000);
}
</script>
</head>
<body>
<form>
<input type="button" value="Display timed text!" onClick="timedText()">
<div id="displayDiv">
<p>Click on the button above. It will tell you when two, four, and six seconds have passed.</p>
</div>
</body>
</html>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 01-28-2008, 06:11 PM
BigAlReturns's Avatar
Moderator Extraordinaire!
 
Join Date: Dec 2007
Location: The Wirral, England
Posts: 298
Send a message via MSN to BigAlReturns
Default

That works, but I think the code below is a more elegant way to achieve the same effect - obviously alter the amount added to num each time, and the timeout, to achieve what you want exactly.
Code:
<html>
<head>
<script type="text/javascript">
function timedText(num)
{
document.getElementById('displayDiv').innerHTML=num + ' milliseconds!';
num=num+1;
setTimeout('timedText('+num+')',1);
}
</script>
</head>
<body>
<form>
<input type="button" value="Display timed text!" onClick="timedText(0)">
<div id="displayDiv">
<p>Click on the button above. It will tell you when two, four, and six seconds have passed.</p>
</div>
</body>
</html>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 12:07 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.