Forum Moderators: open
<html>
<head>
<title>Typewriter for Demo</title>
<script>
<!--
var i = 0
var showString= ""
var location = ""
function marquee(location,myString)
{
showString = showString + myString
showString = myString
var stringLength= showString.length
document.show.location.value= document.show.location.value + showString.charAt(i)
i++
var timeID= setTimeout("marquee(location,showString)",100)
if (i >= stringLength)
{clearTimeout(timeID); i=0}
}
//-->
</script>
</head>
<body>
<form name="show">
<input type="text" name="text1" value="" onClick="javascript:marquee('text1','0123456');">
<input type="text" name="text2" value="" onClick="javascript:marquee('place2','9999999');">
</form>
</body>
</html>
Code that works but will only print to the first textfield even if you click on the second textfeild is this:
<html>
<head>
<title>Typewriter for Demo</title>
<script>
<!--
var i = 0
var showString= ""
function marquee(myString)
{
showString = showString + myString
showString = myString
var stringLength= showString.length
document.show.text1.value= document.show.text1.value + showString.charAt(i)
i++
var timeID= setTimeout("marquee(showString)",100)
if (i >= stringLength)
{clearTimeout(timeID); i=0}
}
//-->
</script>
</head>
<body>
<form name="show">
<input type="text" name="text1" value="" onClick="javascript:marquee('0123456');">
<input type="text" name="text2" value="" onClick="javascript:marquee('9999999');">
</form>
</body>
</html>
Can anyone please help?
document.show.location will cause the browser to look for an element whose name is 'location', and that's not what you want. There are a couple different ways you can solve this... One way would be to use eval() to get the correct object. You could also give the text fields an ID, and use document.getElementByID (or document.all for old IE) as a reference to the text area... Or, you can simply change the way you refer to the text areas, which is what I did below. <html>
<head>
<title>Typewriter for Demo</title>
<script>
<!--
var i = 0
var showString= ""
var location = ""
function marquee(location,myString)
{
showString = showString + myString
showString = myString
var stringLength= showString.length
document.forms[0].elements[location].value= document.forms[0].elements[location].value + showString.charAt(i)
i++
var timeID= setTimeout("marquee(location,showString)",100)
if (i >= stringLength)
{clearTimeout(timeID); i=0}
}
//-->
</script>
</head>
<body>
<form name="show">
<input type="text" name="text1" value="" onClick="javascript:marquee(0,'0123456');">
<input type="text" name="text2" value="" onClick="javascript:marquee(1,'9999999');">
</form>
</body>
</html>
if (document.getElementById(myName).value==myString)
{
document.getElementById(myName).value="";
}
document.getElementById(myName).value=document.getElementById(myName).value + showString.charAt(i)
//document.show.text1.value= document.show.text1.value + showString.charAt(i)
i++
var timeID= setTimeout("marquee(showName,showString)",100)
if (i >= stringLength)
{clearTimeout(timeID); i=0}
}
//-->
</script>
</head>
<body>
<form name="show">
<input type="text" name="text1" id="text1" value="" onClick="javascript:marquee(this.name,'0123456');">
<input type="text" name="text2" id="text2" value="" onClick="javascript:marquee(this.name,'test');">
</form>
</body>
</html>
When the document loads I want all the fields in the form to be populated with the information that I'm currently pass when I do the onClick command. Anyone kno whow to help. Thanks.
<html>
<head>
<title>Typewriter for Demo</title>
<script>
<!--
var i = 0
var showString = ""
var showName = ""
function marquee(myName,myString)
{
showName = myName
showString = myString
var stringLength= showString.length
if (document.getElementById(myName).value==myString)
{
document.getElementById(myName).value="";
}
document.getElementById(myName).value=document.getElementById(myName).value + showString.charAt(i)
//document.show.text1.value= document.show.text1.value + showString.charAt(i)
i++
var timeID= setTimeout("marquee(showName,showString)",100)
if (i >= stringLength)
{clearTimeout(timeID); i=0}
}
//-->
</script>
</head>
<body onload="marquee('text1','0123456');marquee('text2','test');">
<form name="show">
<input type="text" name="text1" id="text1" value="">
<input type="text" name="text2" id="text2" value="">
</form>
</body>
</html>
DrDoc thanks for the help so far. I really appreciate it!
<body onload="marquee('text1','0123456');marquee('text2','test');"> This calls the function twice. You need to remember that every _Timeout is a new thread, so the 2nd function call will be executed straight after the first timer has been set in the first call. Since quite a few variables used are global, there will be a fair amount of interference. We need to keep everything passed 'within the loop', without using global vars.
function marquee(boxId,text,i)
{
i = i¦¦0
var box = document.getElementById(boxId)
if(i==0) box.value = ""
box.value += text.charAt(i)
if(++i < text.length)
setTimeout("marquee('"+boxId+"','"+text+"',"+i+")",100)
}// alternative
function marquee(boxId, text)
{
var i = 0
var box = document.getElementById(boxId)
var str = ""
box.value = ""
repeat()
function repeat()
{
str += text.charAt(i++)
box.value = str
if(i<text.length)
setTimeout(repeat,100)
}
}
The 'standard' version gets trickier to play with, as you can see (or maybe it just gets that way when I do it).
In both versions, you add the statement to execute later as an optional last argument, in the form of an anonymous function, but, in the 'standard' ver, if you are adding such a function, you also need to fill in the 'i' argument with a zero, to fill the place. In the 'alternative' you don't need to do that.
STANDARD
marquee('test1','test text',0, function(){alert('finished')})
// or
marquee('test1','test text',0,function(){ marquee('test2','more text')}) ALTERNATIVE
marquee('test1','test text',function(){alert('finished')})
// or
marquee('test1','test text',function(){ marquee('test2','more text')}) A handy tip:
If you want to use a long string, you can assign it to a variable in the script, then put the variable into the call in marquee(). You'd probably already guessed that. You can do the same thing with a complicated function.
-- script --var text = "_a_very_long_string_"
function doThings()
{
// lots off stuff
}-- fn call --
marquee('test1',text,doThings)
// STANDARD (replace ¦¦ again)function marquee(boxId,text,i,nextFn)
{
i = i¦¦0
var box = document.getElementById(boxId)
if(!box.marquee_nextFn)
box.marquee_nextFn = nextFnif(i==0) box.value = ""
box.value += text.charAt(i)
if(++i < text.length)
setTimeout("marquee('"+boxId+"','"+text+"',"+i+")",100)
else if(box.marquee_nextFn)
{
box.marquee_nextFn()
box.marquee_nextFn = null
}
}// ALTERNATIVE
function marquee(boxId, text, nextFn)
{
var i = 0
var box = document.getElementById(boxId)
var str = ""
box.value = ""
repeat()
function repeat()
{
str += text.charAt(i++)
box.value = str
if(i<text.length)
setTimeout(repeat,100)
else if(nextFn)
nextFn()
}
}
This is my function:
function marquee(boxId, text, nextFn)
{
var i = 0
var box = document.getElementById(boxId)
var str = ""
box.value = ""
repeat()
function repeat()
{ str += text.charAt(i++)
box.value = str if(i<text.length)
setTimeout(repeat,100)
else
if(nextFn)
nextFn()
}
}
I get an error message saying Object Expected on the on the line where the body onload command is. Any idea why this woudl be the case?
I should mention that, being lazy, I don't put in semi-colons at the end of statements most of the time. This, combined with WebmasterWorld's formatting quirks, can duck up scripts a little. For instance, the function you posted had a line:
box.value = str if(i<text.length) Which should be:
box.value = str;
if(i<text.length) It may be something as simple as that.