Using Firefox, I am trying to access a link within my HTML document, then use addEventListene r() to add a click event to that link. I am using two external JS files.
ajaxModal.js has the listener function which will be called when the link is clicked.
events.js will have all the addEventListene r() functions.
ajaxModal.js contents:
events.js contents:
HTML:
[HTML]
<html>
<head>
...
<script type="text/javascript" src="scripts/events.js"></script>
<script type="text/javascript" src="scripts/ajaxModal.js"></script>
...
</head>
<body>
...
<div id="nav">
<ul>
<li><a href="index.htm l" onclick="showAj axModal(); return false">Home</a></li>
<li><a id="page1" href="page1.htm l">Page 1</a></li>
<li><a href="page2.htm l">Page 2</a></li>
<li><a href="page3.htm l">Page 3</a></li>
</ul>
</div>
...
</body>
</html>
[/HTML]
The inline event handler works, and runs showAjaxModal() ....
the second link with ID 'page1' does not run showAjaxModal() , spite the fact that I added a click event to it with addEventListene r in events.js.
I am almost sure this is a beginner mistake. How can I get the addEventListene r() to work on this page?
Thanks,
Jeremy
ajaxModal.js has the listener function which will be called when the link is clicked.
events.js will have all the addEventListene r() functions.
ajaxModal.js contents:
Code:
function showAjaxModal()
{
xmlHttp = GetXmlHttpObject();
if(xmlHttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
alert('Accessed showAjaxModal!');
}
// Detect XMLHTTP
function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
events.js contents:
Code:
var triggerLink = document.getElementById('page1');
triggerLink.addEventListener('click',showAjaxModal,false);
[HTML]
<html>
<head>
...
<script type="text/javascript" src="scripts/events.js"></script>
<script type="text/javascript" src="scripts/ajaxModal.js"></script>
...
</head>
<body>
...
<div id="nav">
<ul>
<li><a href="index.htm l" onclick="showAj axModal(); return false">Home</a></li>
<li><a id="page1" href="page1.htm l">Page 1</a></li>
<li><a href="page2.htm l">Page 2</a></li>
<li><a href="page3.htm l">Page 3</a></li>
</ul>
</div>
...
</body>
</html>
[/HTML]
The inline event handler works, and runs showAjaxModal() ....
the second link with ID 'page1' does not run showAjaxModal() , spite the fact that I added a click event to it with addEventListene r in events.js.
I am almost sure this is a beginner mistake. How can I get the addEventListene r() to work on this page?
Thanks,
Jeremy
Comment