Results 1 to 7 of 7

Thread: Very simple JS function problem

  1. #1
    Join Date
    Jun 2008
    Posts
    16
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Very simple JS function problem

    I have a very simple function which I cannot figure out.

    Here's the JS
    ----
    function replaceHeading() {
    var heading = document.getElementById("initial_heading");
    alert(heading.nodeName);
    }
    ----
    and here's the relevant HTML
    ----
    <div id="content">
    <h1 id="initial_heading" class="specialh1">[xyz page]</h1>
    <p><img id="temp" src="images/zone.jpg" alt="Zone sign" width="400" height="267" /></p>
    <h1 class="specialh1"><a href="index.html">[.com]</a></h1>
    </div>
    ----
    shouldn't alert(heading.nodeName) return h1? I get no JS message at all when I load the page.

  2. #2
    Join Date
    Jul 2008
    Posts
    128
    Thanks
    0
    Thanked 17 Times in 16 Posts

    Default

    Quote Originally Posted by tk403 View Post
    I
    shouldn't alert(heading.nodeName) return h1? I get no JS message at all when I load the page.
    Are you actually calling your function anywhere?
    It has to be called after the relevant element has rendered.

  3. #3
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    This function you provided should work. Here is some code I created for you to replace it:

    Code:
    <!-- your doctype up here -->
    <html>
    <head>
    <title></title>
    <script type="text/javascript">
    function replaceHeading() {
    window.alert(document.getElementById("initial_heading").nodeName);
    }
    </script>
    <style type="text/css">
    </style>
    </head>
    <body onload="replaceHeading()">
    <div id="content">
    <h1 id="initial_heading" class="specialh1">[xyz page]</h1>
    <p><img id="temp" src="images/zone.jpg" alt="Zone sign" width="400" height="267" /></p>
    <h1 class="specialh1"><a href="index.html">[.com]</a></h1>
    </div>
    </body>
    </html>
    This, in my browser (IE8 Beta), works and alerts "H1". You probably didn't inlude onload="replaceHeading()" in the body tag.

    -magicyte
    Last edited by magicyte; 11-10-2008 at 10:37 PM.

  4. #4
    Join Date
    Jun 2008
    Posts
    16
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default

    Thanks!!

    I did call the function initially, but after the close of the function. I had:

    window.onload = replaceHeading();

    after I closed out the function. I also have to call the function from the <body> tag?

  5. #5
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    after I closed out the function. I also have to call the function from the <body> tag?
    No. You can do it either way. onload in the body tag is just for HTML. You can do it in JavaScript too, but don't do it at the same time.

    You are welcome.

    Edit: Made a clumsy error.

    -magicyte
    Last edited by magicyte; 11-10-2008 at 10:37 PM.

  6. #6
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    Quote Originally Posted by magicyte View Post
    window.onload = replaceHeading(); best be document.onload = replaceHeading;

    -magicyte
    Mind explaining why?

    I don't think document.onload will work:
    Code:
    document.onload=test;
    function test()
    {
    alert('Hello World');
    }
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  7. #7
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    Quote Originally Posted by rangana
    Mind explaining why?
    My mistake. It IS window.onload.

    -magicyte

Bookmarks

Posting Permissions

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