...

View Full Version : Function output in another function using id



bradymc
01-01-2007, 04:20 PM
I have an external JS file/function that does a calculation based on numbers entered into a form. If the output of the function equals 8 characters, then I want to control the display of it's output so that it looks like a typewriter or decryption effect. If not, then it would just give an error message. The variable that holds the information is 'sn' inside of functionXYZ().

Now to the actual question....
I found a couple of cut-and-paste JS that do what I want, but can't seem to get my results to display. In the HTML doc, they use a
<div id="whatever">Text that displays with effects here.</div> The id tells the JS what text to display. I assumed that I could just change it to read
<div id="whatever"><script>functionXYZ();</script></div>, but that displays "functionXYZ();" and then my results.

I should explain that I currently have the if-then-else setup as:

if (sn.length == 8){
document.write(sn)
else {
document.write('ERROR')
};


Someone told me that I really shouldn't use document.write for this situation, but didn't give me any alternatives.

Here's a link to the decrypting text I wanted to use: Decrypting Text (http://i-code.co.uk/javascript/decryptingtext.php)

One other smaller problem is that I would like the decrypting effect to only use random numbers and NOT random characters. I thought I could just change all the 'char' variables to 'num'. Obviously, that didn't work.

Any help is appreciated!

Mr J
01-01-2007, 07:44 PM
One alternative to

if (sn.length == 8){
document.write(sn)
else {
document.write('ERROR')
};

is

if (sn.length == 8){
document.getElementById("whatever").innerHTML=sn
else{
document.getElementById("whatever").innerHTML="ERROR"
}

1212jtraceur
01-01-2007, 08:12 PM
If you post the remainder of your code, I could help a bit more thoroughly. However, I will attempt anyway:

Instead of document.write, you can use textElement.nodeValue. So if you put a bit of text inside your div#whatever, you can change it using:
document.getElementById('whatever').firstChild.nodeValue = 'newText';

As for your smaller problem, use this for your script (but replace the innerHTML-s with the above solution to document.write), and it should generate only numbers during the 'decryption process'.



var got;
var chars;

function change()
{
var randstring = "";
var rslength = chars.length - got.length;

var decrypted = document.getElementById("decoded");
var encrypted = document.getElementById("encoded");

for(var x=0;x<rslength;x++)
{
i = Math.floor(Math.random() * 9);
randstring += i
}
encrypted.innerHTML = randstring;

got = chars.substring(0, got.length + 1);
decrypted.innerHTML = got;


if(chars.length > got.length)
{
setTimeout("change()", 10);
}
else
{
encrypted.innerHTML = "";
}
}

function startdecrypt()
{
var decrypted = document.getElementById("decoded");
var encrypted = document.getElementById("encoded");

chars = decrypted.innerHTML;
decrypted.innerHTML = "";
got = "";
setTimeout("change()", 10);
}

Mr J
01-01-2007, 10:10 PM
Just an observation here but bradymc might get even more confused by throwing in DOM methods when in this instance innerHTML will do

Pyth007
01-02-2007, 09:29 PM
I agree that Mr J's method will be easier... Just remember to have a unique id for the <div> that you want to use so that getElementById() can find it! Also, the reason why people say not to use document.write is that this gets run before the page is completely loaded. Thus it becomes difficult to get well-formed HTML. Also because it is written to the document before its loaded, it isn't dynamic; using DOM methods like getElementById allows your scripts to dynamically change while the document is up and running.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum