Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Aug 2006
    Location
    Dallas area
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Arrow Function output in another function using id

    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
    Code:
    <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
    Code:
    <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:
    Code:
    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

    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!

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    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"
    }
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #3
    Regular Coder
    Join Date
    Oct 2006
    Posts
    206
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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:
    PHP Code:
    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'.

    PHP Code:
    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++)
        {
            
    Math.floor(Math.random() * 9);
            
    randstring += i
        
    }
        
    encrypted.innerHTML randstring;

        
    got chars.substring(0got.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);


  • #4
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Just an observation here but bradymc might get even more confused by throwing in DOM methods when in this instance innerHTML will do
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #5
    Regular Coder
    Join Date
    Sep 2005
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    If you want answers, write a smart question.

    Yes, someone probably does know how...

    Oh, and if you want to learn, STFW!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •