Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JavaScript error trying to add event listeners using DOM

Status
Not open for further replies.

donpro

IS-IT--Management
Jun 15, 2004
2
Hi,

I have PHP file called file1.php with an empty <div></div> in the middle. I've added a 'load' event listener so that on page load, it calls an AJAX function that calls file2.php. file2.php creates a table and loads it into the <div></div> in file1.php.

file2.php has <a> tags in it's <th> columns and I wish to trap when a user clicks on the column heading. The old way was to use the onclick() method but I'd rather use event listeners.

Here's my problem. I am getting "obj has no properties" when trying to add those listeners. I *think* it is because the code in file2.php isn't part of the original DOM tree for file1.php. So how can I use event listeners instead of onlclick()? I know I can add top the DOM by creating elements and appending them but that adds to the HTML code
and my code is already in place via the AJAX call.

What to do...? DO I just use the old passe onclick() method?
 
You know that onclick is an event listener, right?

If it works with onclick I'd leave it be - using onclick isn't inefficient or considered bad programming practice, so I'm not sure why you feel the need to change them. However, if you really want to get rid of them then you're going to have to make sure that when you assign the listener to the anchor objects that they exist on the page. If you're assigning the listeners when the page loads then that won't work - the reason being is that AJAX doesn't even load the anchors into the div until after the page has loaded. You're going to need to formulate the code to ensure the listeners aren't assigned until after the page has loaded, and then after the AJAX routine has completed.

Personally, I'd just stick with onclick....

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B> bites again.[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top