1 Replies - 384 Views - Last Post: 25 February 2010 - 10:02 PM

#1 caffeinejunkie42  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 13-May 09

replace DOM nodes

Posted 25 February 2010 - 04:23 AM

Hey everyone! I'm pretty new to Javascript. I'm working on an assignment and could use some help. Pretty basic--we need a counter and a button in a <div> element, click the button and the counter advances. The topic we're covering is DOM nodes, so that's the method I need to use to update the counter.

The problem I'm having is the code I wrote works perfectly fine in the editor (Komodo), it works in Firefox and it works in Safari. However, I get an error every time I try to run it in IE8. I tried looking at the "Developer Tools", and it says the "onclick" function in the <button> is an invalid argument.

Anyway, here's the code...any ideas? Thanks!

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Chapter 12 Homework - Troy Christensen</title>
    <style type= "text/css">
        h1 {
            font-size: 50pt;
            padding: 0px;
            margin: 0px;}
    </style>
    
    <script type= "text/javascript">
    <!--
        var count = 0;
        
        function replaceNode()
        {
            var currentNode = document.getElementById("counter");
            var newNode = createNewNode(count);
            document.getElementById("division").replaceChild(newNode, currentNode);
        }
        
        function createNewNode(text)
        {
            var newNode = document.createElement("h1");
            newNode.id = "counter";
            newNode.appendChild(document.createTextNode(text));
            count++;
            return newNode;
        }
    //-->           
    </script>
</head>
<body onload= "replaceNode();">
    <div id= "division">
        <h1 id= "counter"></h1>
        <button type= "button" id= "button" value= "Click to Count" onclick= "replaceNode();" >Click to Count</button>
    </div>
</body>
</html>



Is This A Good Question/Topic? 0
  • +

Replies To: replace DOM nodes

#2 caffeinejunkie42  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 13-May 09

Re: replace DOM nodes

Posted 25 February 2010 - 10:02 PM

OK, I ended up getting the answer from a friend of mine. In case anyone's interested--turns out the function I wrote called replaceNode() is already a Javascript method. This method requires a parameter, so when I called it from the button with no arguments, IE spat out an "Invalid Argument" error.

No idea why the script worked fine in the other browsers, though...one would think if that was already a method, it would have caused a similar error on all browsers. Also no idea why the error occurred on the button click and not on the body "onload". Just weird IE quirks, I guess :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1