How can i import javascript in php? No matter what i try, i can't seem to get the buttons in my page to trigger the events in javascript.
This is my javascript file content:
function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var colCount = table.rows[0].cells.length;
for(var i=0; i<colCount; i++) {
var newcell = row.insertCell(i);
newcell.innerHTML = table.rows[0].cells[i].innerHTML;
//alert(newcell.childNodes);
switch(newcell.childNodes[0].type) {
case "text":
newcell.childNodes[0].value = "";
break;
case "checkbox":
newcell.childNodes[0].checked = false;
break;
case "select-one":
newcell.childNodes[0].selectedIndex = 0;
break;
}
}
}
and
function deleteRow(tableID) {
try {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
for(var i=0; i<rowCount; i++) {
var row = table.rows[i];
var chkbox = row.cells[0].childNodes[0];
if(null !== chkbox && true === chkbox.checked) {
if(rowCount <= 1) {
alert("Cannot delete all the rows.");
break;
}
table.deleteRow(i);
rowCount--;
i--;
}
}
}catch(e) {
alert(e);
}
}
This is how i try to import it in a php file:
<HEAD>
<script src="myfile.js" type="text/javascript"></script>
</HEAD>
what am i doing wrong?
Open up the page in Firefox. Right click the page and select the option to show the sourcecode. Find the script tag and click the linked src. If it gives you a 404, the link is wrong. If it shows your JS code, it's fine. Install Firebug for Firefox and debug your code with it. Make sure your Event Listeners actually get triggered.
make sure that your src attribute is pointing to the correct location of the JS file. (maybe it needs to be "/myfile.js" if the JS file is located under your document root, but the php file isn't).