Web Analytics Made Easy -
StatCounter function won't work - CodingForum

Announcement

Collapse
No announcement yet.

function won't work

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • function won't work

    I'm new to JS and I can't get this function to display. Can you help me please?
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta name="generator" content="PSPad editor, www.pspad.com">
    <title>Untitled</title>
    <head>
    <script type="text/javascript">
    function creatediv(){
    var nwdiv=document.createElement('div');
    nwdiv.style.float='right'
    nwdiv.id='mydiv';
    var txt='hello world!';
    document.body.apendChild(nwdiv);
    document.getElementById(mydiv).innerHTML=txt;
    document.write(txt);
    }
    </script>
    <style type="text/css">
    </style>
    </head>
    <body>
    <form>
    <input type="button" value="Click me!" onclick="creatediv()" />
    </form>
    
    
    </body>
    </html>

  • #2
    apendChild(nwdiv);

    should be

    appendChild(nwdiv);
    - Firebug is a web developers best friend! - Learn it, Love it, use it!
    - Validate your code! - JQ/JS troubleshooting
    - Using jQuery with Other Libraries - Jslint for Jquery/other JS library users

    Comment


    • #3
      Thanks. Made the change and still no hello world. Any other ideas?

      Comment


      • #4
        Code:
        document.getElementById('mydiv')
        ... and get rid of the document.write() stuff

        Comment


        • #5
          Code:
          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
          <html lang="en">
          <head>
          <meta http-equiv="content-type" content="text/html; charset=utf-8">
          <meta name="generator" content="PSPad editor, www.pspad.com">
          <title>Untitled</title>
          <head>
          <script type="text/javascript">
          function creatediv(){
              var nwdiv=document.createElement('div');
              nwdiv.style.float='right'
              nwdiv.id='mydiv';
              var txt='hello world!';
              document.body.appendChild(nwdiv);
              document.getElementById('mydiv').innerHTML=txt;
          }
          </script>
          <style type="text/css">
          </style>
          </head>
          <body>
          <form>
          <input type="button" value="Click me!" onclick="creatediv()" />
          </form>
          
          
          </body>
          </html>
          document.body.apendChild(nwdiv);
          document.getElementById(mydiv).innerHTML=txt;

          document.body.appendChild(nwdiv);
          document.getElementById('mydiv').innerHTML=txt;
          Last edited by DaveyErwin; Sep 5, 2011, 11:26 AM.

          Comment


          • #6
            still an obstacle illusion (no hello world). Here's the current funtion:
            Code:
            function creatediv(){
            var nwdiv=document.createElement('div');
            nwdiv.style.float='right';
            nwdiv.id='mydiv';
            var txt='hello world!';
            document.body.appendChild(nwdiv);
            document.getElementById('mydiv');
            }

            Comment


            • #7
              Originally posted by niche View Post
              still an obstacle illusion (no hello world). Here's the current funtion:
              Code:
              function creatediv(){
              var nwdiv=document.createElement('div');
              nwdiv.style.float='right';
              nwdiv.id='mydiv';
              var txt='hello world!';
              document.body.appendChild(nwdiv);
              document.getElementById('mydiv');
              }
              document.getElementById('mydiv').innerHTML = txt;

              Comment


              • #8
                Success! Except, no float right. Any ideas?

                Also, deleting "document.getElementById('mydiv').innerHTML = txt;" produced same result. I don't know why. do you?

                Comment


                • #9
                  Code:
                  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
                  <html lang="en">
                  <head>
                  <meta http-equiv="content-type" content="text/html; charset=utf-8">
                  <meta name="generator" content="PSPad editor, www.pspad.com">
                  <title>Untitled</title>
                  <head>
                  <script type="text/javascript">
                  function creatediv(){
                      var nwdiv=document.createElement('div');
                      nwdiv.setAttribute("style","float:right;")
                      nwdiv.id='mydiv';
                      var txt='hello world!';
                      document.body.appendChild(nwdiv);
                      document.getElementById('mydiv').innerHTML=txt;
                  }
                  </script>
                  <style type="text/css">
                  </style>
                  </head>
                  <body>
                  <form>
                  <input type="button" value="Click me!" onclick="creatediv()" />
                  </form>
                  
                  
                  </body>
                  </html>

                  Comment


                  • #10
                    Cool. Like I said, I'm new to JS. I understand there's a way to display the html/css in a pop-up. I thought the alert would've done that, but obviously it was displayed in the flow. How would I use the alert to display the html/css in a popup?

                    Comment


                    • #11
                      Code:
                      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
                      <html lang="en">
                      <head>
                      <meta http-equiv="content-type" content="text/html; charset=utf-8">
                      <meta name="generator" content="PSPad editor, www.pspad.com">
                      <title>Untitled</title>
                      <head>
                      <script type="text/javascript">
                      function creatediv(){
                          var nwdiv=document.createElement('div');
                          nwdiv.setAttribute("style","float:right;")
                          nwdiv.id='mydiv';
                          var txt='hello world!';
                          document.body.appendChild(nwdiv);
                          document.getElementById('mydiv').innerHTML=txt;
                      alert(document.body.innerHTML)
                      }
                      
                      </script>
                      <style type="text/css">
                      </style>
                      </head>
                      <body>
                      <form>
                      <input type="button" value="Click me!" onclick="creatediv()" />
                      </form>
                      
                      
                      </body>
                      </html>

                      Comment


                      • #12
                        No pop-up. What do you think?

                        Comment


                        • #13
                          But you definitely see a user dialog showing you the HTML source of the page, don't you?

                          Comment


                          • #14
                            Got it now! Thank-you very much.

                            Now i'm making sure I understand the function and I'm OK util I get to:
                            document.body.appendChild();

                            I can't find the documentation that actually says appendChild() is a method of the property body and body is a property of the document object.

                            Am I thinking about this correctly?

                            Comment


                            • #15
                              document.body is actually a "short cut" to document.getElementsByTagName('body')[0] and exposes this DOM element to Javascript. appendChild() is a method for each DOM element and appends the element given as a parameter to the element the method is applied to.

                              Comment

                              Working...
                              X
                              😀
                              🥰
                              🤢
                              😎
                              😡
                              👍
                              👎