tags:

views:

15

answers:

1

My first start into JavaScript. I can make mouseover events trigger changes when I use it with images, but not with a div only containing text. What am I doing wrong ? Code follows.

<html>
    <head>
      <script language="javascript" type="text/javascript">
        function addHeading(node, text) {
          var newNode=document.createElement("h1");
          var text=document.createTextNode(text);
          newNode.appendChild(text);
          node.appendChild(newNode);
        }
      </script>
    </head>

    <body>
      <div onmouseover="addHeading(document.body, "Header added")">
      Hola!
      </div>
    </body>
</html>
A: 

It should be (note the apostrophes):

<div onmouseover="addHeading(document.body, 'Header added')">

otherwise you close the attribute value prematurely. Having that said, it works for me.

Alexander Gyoshev
Thanks for pointing out my silly mistake. Guess I will be looking for a JavaScript syntax checker :)
Michael
welcome :) this particular problem can be caught through HTML validation -- http://validator.w3.org/
Alexander Gyoshev