i saw question , first answer works great question asked... if want re-use code on , on different named elements?
let me specific.
throughout code (multiple pages on site calling same file.js) want have various internal "links" right have written as:
<a onclick="testfunction('targeta');">test a</a> <a onclick="testfunction('targetb');">test b</a> and calls javascript accordingly. advantage same js used on , on without need write 1 each "target". disadvantage, besides fact have type mess above each time, according guy's answer isn't right way things.
so there "right way" can (realize pseudo-code):
<a id="test.a">test a</a> <a id="test.b">test b</a> and not have explicitly define test.a vs test.b (vs c, d, e...) in js file? example, js registered "#test" parse out .x , act on variable?
or????
i dont know if wildly exaggerated, perhaps not understanding want -
var tests = document.queryselectorall('[id^="test."]'); [].foreach.call(tests, function(a) { a.addeventlistener('click', function(e) { alert(e.target.id.split('.')[1]+' clicked'); }, false); }) would "b clicked" etc. target test.* id's want. obvious dont have use id in click event, attribute holding value or other things.
demo -> http://jsfiddle.net/sbeyer8f/
Comments
Post a Comment