Hi Nikolaj,

Originally Posted by
niklicht
I guess its about something very basic in JavaScript programming principles...
I think there is alot of confusion on this topic. It is true that we want to physically separate our Js and HTML. However, we should not let that lead us to separate the Js and HTML in our thinking - that is, in our design. Much of the Js we write is for the purpose of a page enhancement: the Js is intended to work with a specific html structure (or a group of related structures). So, we can't really talk about event listeners unless we also talk about the corresponding html.
I do realize that there are times when we develop Js that has nothing to do with the html - but that's not the kind of code we are talking about here.
how the "reusability" of functions called by event handlers is "ported" to the event-listener-way of binding functions to tags?
I see several different implied questions in this one question - altho they all overlap. For example:
1. How event listeners can be associated with specific elements (or specific groups of elements) and how to do this in script (at runtime) instead of using a string of script in an html event attribute;
2. How to associate specific data with specific elements and specific events.
3. How to make the listeners "reusable", that is, how to abstract them somewhat so that they apply to more than just one element and one set of data.
First, let me just give links to two threads where the original code that was posted was using strings of script in html event attributes. In my responses you can see how I redesigned their Js/HTML relationships based on some of the principles we are discussing here.
Here is an example.
Code:
<a href="test1.html" title="test1" onmouseover="javascript:ShowContent('01box');HideContent('mainbox');HideContent('02box');HideContent('03box');HideContent('04box')" onmouseout="javascript:ShowContent('mainbox');HideContent('01box');HideContent('02box');HideContent('03box');HideContent('04box')"><img src="test1.jpg" width="176" height="225" border="0" /></a>
Here is another example.
Code:
<dd><a href="javascript:switchid('topHomes');" title="Top Homes"><strong>Homes</strong></a></dd>
Bookmarks