It works!!! Your post triggered a thought in my mind and I was misplacing where to include the event parameter. The code I posted is calling a function to return the code for another function. Event needed to be added in the function that was being called by the onclick event.
Thank you for the feedback!!!
Here is what I mean (notice event was added as a parameter for the function):
Code:
return function (event) {
//-- Cross browser way to get event
var evt = event || window.event;
alert( "event: " + evt );
//-- Causes events to buggle up, a href goes first
evt.stopPropagation() ? evt.stopPropagation() : evt.cancelBubble = true;
//-- Prevents other events from firing
evt.preventDefault() ? evt.preventDefault() : evt.returnValue = false;
testPassObj(obj);
};
Here is the complete code.
Code:
<html>
<head>
<title>Test</title>
<script type="text/javascript">
function objectX() {
var foo = "";
this.setFoo = function( newFoo ) {
foo = newFoo;
};
this.getFoo = function() {
return foo;
};
}
function testObj() {
var content = document.getElementById('content');
content.innerHTML = '<table><tr onClick="javascript:displayMessage();"><td><a href="#" id="Test1">Test 1</a></td><td><a href="#" id="Test2">Test 2</a></td></tr></table>';
for( var i = 1; i < 3; i++ ) {
var obj = new objectX();
obj.setFoo( "Test " + i );
// pass obj as an argument and receive it as a parameter
// in order to keep a private reference to its current value
document.getElementById("Test" + i).onclick = createTestClickHandler( obj );
}
}
function createTestClickHandler( obj ) {
return function (event) {
//-- Cross browser way to get event
var evt = event || window.event;
alert( "event: " + evt );
//-- Causes events to buggle up, a href goes first
evt.stopPropagation() ? evt.stopPropagation() : evt.cancelBubble = true;
//-- Prevents other events from firing
evt.preventDefault() ? evt.preventDefault() : evt.returnValue = false;
testPassObj(obj);
};
}
function testPassObj( obj ) {
alert( "object: " + obj.getFoo() );
}
function displayMessage() {
alert( "Do not display this message" );
}
</script>
</head>
<body onLoad="testObj();">
<div id="content"></div>
</body>
</html>
Bookmarks