title: Calling a Javascript function from HTML
body: <p>I am having a HTML table with 5 rows (say) which is being displayed on the webpage. The first column of the table is <strong>RowID</strong> (which is unique for each row) and the last column of each row is a <strong>[X]</strong> button (which on click will remove the row). The whole table is in a <strong><code>&lt;div&gt;</code></strong> element with <code>id="cartmain"</code></p>
  
  <pre><code>&lt;div id="cartmain"&gt;
       &lt;table&gt;
            &lt;tr&gt;
              &lt;td&gt;....&lt;/td&gt;
              &lt;td&gt;....&lt;/td&gt;
              &lt;td&gt; &lt;a href="#" onclick="removeitem('id1')"&gt; [X] &lt;/a&gt; &lt;/td&gt;
            &lt;/tr&gt;
            ..
            ..
       &lt;/table&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>This is how I am removing the row:</p>
  
  <p><strong>STEP1:</strong> When user clicks on [X] button, an XMLHTTPRequest function will send message to a PHP code with unique id of each row. During the time PHP code is removing the row, DIV element shows "Loading..."</p>
  
  <p><code>document.getElementById("cartmain").innerHTML = "Loading..."</code></p>
  
  <p><strong>STEP2:</strong> PHP code will delete the row from table (and from database) and will return the remaining table.</p>
  
  <pre><code>&lt;?php
       //removing row from database/table...
  
       //send the remaining table back to the XMLHTTPRequest function...
       echo "&lt;table&gt;";
       echo "&lt;tr&gt;&lt;td&gt;...&lt;/td&gt;
                 &lt;td&gt;...&lt;/td&gt;
                 &lt;td&gt; &lt;a href=\"#\" onclick=\"removeitem('id1')\"&gt; [X] &lt;/a&gt; &lt;/td&gt;";
       echo "&lt;/tr&gt;";
       ...
       ...
       ...
       echo "&lt;/table&gt;";
  ?&gt;
  </code></pre>
  
  <p>This remaining table received from the PHP code is then shown in the DIV element by using the below line:</p>
  
  <p><code>document.getElementById("cartmain").innerHTML = removeitem.responseText;</code></p>
  
  <p><strong>My Problem:</strong>
  Suppose I have table with 5 rows. When I click on the Remove button, the row is removed successfully and table is then displayed with 4 remaining rows. Here comes the problem: When I want to remove another row from the table: Nothing is Happening. In other words, if I again click on <strong>[X]</strong> of some row <strong>then nothing happens</strong>. The XMLHTTPRequest function is not called. In ideal case, it should have called the XMLHTTPRequest function again with that unique RowID and table should then be displayed with remaining 3 rows. </p>
  
  <p>IMPORTANT NOTE: When I refresh the page, then I am able to remove another row. But this time also, I can remove only one row. For removing one more, I have to refresh the page again. </p>
  
  <p>Is anyone able to identify the problem here? Please help. </p>
  
  <p>NOTE: My table actually is a Shopping Cart which contains a product in each row. <strong>[X]</strong> button actually signifies "removing the item" from the cart. </p>
  
  <p>Here is the JavaScript code:</p>
  
  <pre><code>function removeitem(itemid)
  {
      alert("The item to be removed is: "+itemid);
      if(window.XMLHttpRequest)
      {
          removeitem = new XMLHttpRequest();
      }
      else
      {
          removeitem=new ActiveXObject("Microsoft.XMLHTTP");
      }
  
      removeitem.onreadystatechange=function()
      {
          if (removeitem.readyState==4 &amp;&amp; removeitem.status==200)
          {
              document.getElementById("cartmain").innerHTML=removeitem.responseText;
          }
          else if(removeitem.readyState &lt; 4)
          {
              document.getElementById("cartmain").innerHTML="Loading...";
          }
      }
      var linktoexecute = "cart.php?option=5&amp;itemid="+itemid; //option =5 signifies that I want to remove the item with ITEMID.
      removeitem.open("GET",linktoexecute,true);
      removeitem.send();
  }
  </code></pre>
  
  <p>The function removeitem shows an alert "The item to be removed is: 123" for the first time but does not shows second time. When I am checking in Firebug console, the following error is coming second time:</p>
  
  <p><strong>removeitem is not a function</strong></p>
  
  <p>Please help!!</p>
  
  <p>All my Javascript functions are in a separate file (sc.js) for which I am using <code>&lt;script type="text/javascript" src="js/sc.js"&gt;&lt;/script&gt;</code> in the <code>HEAD</code> tag of my page.</p>
  
  <p>My viewpoint: So finally I think the question comes to a simple point: If a webpage is requesting some HTML from a PHP page using XMLHTTPRequest -- and if that HTML (sent by PHP) contains some button which is calling a Javascript function -- then what will happen in this situation[?]. Now since I am able to remove the row for the first time, I think it is false to say that above situation will not work. It is just that the code that came from PHP and the Javascript which has already been loaded are not aware of each others presence when I click the button second time. </p>