I have a web project with a menu item that needs to call a page, cusomers.php and a javascript function associated with the menu selection. I have chosen to use pure javascript as opposed to jquery. The menu piece is located in include/adminheader.php and looks like this:
<li><a href="#">View</a>
<ul>
<li><a id="allCustomers" value="all" href="customers.php" >
All Customers</a></li>
<li><a href="customers.php" id="singleCustomer" value="single" >
Single Customer</a></li>
</li>
When the event listener is setup in the external file with ‘onload’:
window.onload = function() {
document.getElementById("allCustomers").addEventListener("click", getCustomers);
document.getElementById("singleCustomer").addEventListener("click", viewCustomer);
}
The event listener is registered and the js function fires with one click, but the page link isn’t followed (i.e. if I’m in the index page when I click menu link to ‘All Customers’ I’m still on index.php after the menu click).
When I setup the same code at the bottom of customers.php the menu link takes me to customers.php but I have to click the link twice the first time, once thereafter to call the js function.
I have also tried adding the listener to the surrounding <li>
tag to call the js function and had to click twice the first time for it to fire.
I have read the MDN reference here: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener, and the information https://www.w3.org/TR/DOM-Level-3-Events/#event-flow, and of course many stack overflow entries to try to understand how to fix this but am still lost.
I have seen similar questions regarding a js function having to be clicked twice the first time and this was fixed with the onload, but never in conjunction with a link to another page (instead of a href="#"). I’m wondering if this might have something to do with capture but couldn’t figure that out. I realize that I probably should have been able to recognize that another question addresses my problem but if I saw it I did not recognize how to apply to my situration. Please help.
EDIT - js code added need to navigate to page prior to function call
function getCustomers() {
event.preventDefault();
grid.innerHTML = "";
outputTable.innerHTML = "";
grid.className = "grid_4cols";
var ajax = new XMLHttpRequest();
ajax.open("GET", "../controller/admincontroller.php?getCustomers=all", true);
ajax.send();
ajax.onreadystatechange = function() {
if (ajax.readyState == 4 && ajax.status == 200) {
var arr = JSON.parse(ajax.responseText);
var result = "";
for (var i = 0; i < arr.length; i++) {
result += "<form action='../controller/admincontroller.php' method='POST' >";
result += "<input type='hidden' id='id' name='id' value ='" + arr[i]['ID'] + "'>" ;
result += "<input type='text' name='fname' value='" + arr[i]['fname'] + "'>" ;
result += "<input type='text' name='lname' value='" + arr[i]['lname'] + "'>";
result += "<input type='text' name='email' value='" + arr[i]['email'] + "'>";
result += "<input type='submit' name='selectCustomer' value='Select' ></form>";
}
grid.innerHTML = result;
}
};
}
//not implemented. Not sure how to setup so destination DOM is
//loaded before function call to getCustomers() or viewCustomer()
function goToCustomers() {
location.assign("customers.php"); //used location.replace for first edit
}