0

In the XHTML page i have this div:

<div id="listAzioni">
    <h4 style="margin-bottom:3px;">List shape :</h4>
</div>

Subsequently incorporated within the div an input element via javascript using the library innerXhtml.

var paper = document.getElementById("listAzioni");  
var toWrite = "<h4 style=\"margin-bottom:3px\">List shape :</h4>";  
toWrite += "<input type=\"button\" class=\"listButton\" value=\""+id+"\" onclick=\"showAction('"+id+"');\"/>"+'<br/>'; 
innerXHTML(paper,toWrite);

But after adding the input element has no onclick attribute. I tried so hard but I have not found anything that would solve my problem. Where am I doing wrong?

LuckyStarr
  • 1,468
  • 2
  • 26
  • 39

1 Answers1

0

Which browsers have you tried?

I suggest you to use standard DOM APIs, I write your code as below:

<html>
  <head>
    <style type="text/css">
    .button {
      border: 1px solid black;
      padding: 3px;
    }
    </style>
    <script type='text/javascript'>
      function addButton() {
        var container = document.getElementById('container');
        if (container != null && container != undefined) {
          var child = document.createElement("div");
          var button = document.createElement("input");

          button.value = "Click to alert";
          button.type = "button";
          child.style.padding = "10px";
          child.style.border = "2px solid red";    
          button.className = "button";

          button.onclick = showAlert;
          child.appendChild(button);
          container.appendChild(child);
        }
      }

      function showAlert() {
        alert("you clicked the button!");
      }
    </script>
  </head>
  <body>
    <div id="container"></div>

    <input value="add div and button" type='button' onclick='addButton();'/>
  </body>
</html>
ehsun7b
  • 4,796
  • 14
  • 59
  • 98
  • I tried my code in FF and Chrome, but both give me this error. I also thought I had the alternative proposal from you but I try to run the current code without changing anything. – LuckyStarr Jan 14 '11 at 13:05
  • What was the result of my code? I always suggest you to use standard APIs, and if you feel that JavaScript is missing some basic features make a choice to use a basic complement to JavaScript such as Prototype or jQuery. – ehsun7b Jan 15 '11 at 06:59