Jump to content

My first javascript function (pllease don't laugh ;)


fortnox007

Recommended Posts

Hi all  ::)

 

Sorry in advance for my ignorance, but I just started learning java script today.

I wrote a little script with a condition. If the condition is true the stuff inside a <p> element should be altered, but For some reason I can't see how to do this I looked up for the DOM, but i didn't find <p> in there. (is it even possible?)

 

If someone maybe has an idea on how to achieve this (or maybe a nice alternative)please let me know, All tips and hints are welcome. (I ordered a book today, so I'll post less noobish scripts in the future)

 

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
        <head>
            <title>JAVA SCRIPT TEST PAGE</title>
            <script type="text/javascript">
               function my_first_function(){
                   var text1 = 'monkeys';
                   if (document.getElementById(input)!= text1){
                       document.write(document.getElementById(p_element)='wrong text');
                   }else{
                       document.write(document.getElementById(p_element)='your correct');
                   }
               }
            </script>
        </head>
            <body>   
                <input type="text" size="30" id="input" onchange="my_first_function()" value="" />
                <br />
                <p id="p_element">This is just default text.</p>
            </body>
    </html>

Link to comment
Share on other sites

Not far off..

 

               function my_first_function(){
                   var text1 = 'monkeys';
                   if (document.getElementById('input').value != text1){
                       document.getElementById('p_element').innerHTML = 'wrong text';
                   }else{
                       document.getElementById('p_element').innerHTML = 'your correct';
                   }
               }

Link to comment
Share on other sites

Oh wow! ty guys, I have been searching my butt off.

May I Ask 1 more thing.

I have the feeling if I use document.write(); the page refreshes is that true? I haven't found other ways of printing text besides that and the innerHtml now : )

 

Anyways thanks alot and sorry for the noobie script I wrote.

 

-edit: Yeah i think document.write really refreshes the page. I have please an extra piece in the function to test

 

document.write('lalalalalaa'); 

 

When this gets executed the page turns blank and tels lalalalalaa (hmm maybe that's because the function is in between the header)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.