Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
49
rated 0 times [  56] [ 7]  / answers: 1 / hits: 51868  / 10 Years ago, fri, may 23, 2014, 12:00:00

I'm building a decision tree in JavaScript. I do not have jQuery available to me for this project.



I would like to be able to have buttons, placed anywhere in the decision tree (Hidden or displayed anywhere on the page), with the same class name. The listener on the JS side would then run a function.



Here is what I am using for and ID based listener. It works well but I need to be able to have multiple buttons with the same class or name available. Although I have seen examples of this, I cannot get it to function properly.



function q1a1() {
var q1a1button = document.getElementById(q1answer1);
if(q1a1button.addEventListener){
q1a1button.addEventListener(click, function() { q1answer1();}, false);
} else if(q1a1button.attachEvent){
q1a1button.attachEvent(onclick, function() { q1answer1();});
}
};

if(window.addEventListener){
window.addEventListener(load, q1a1, false);
} else if(window.attachEvent){
window.attachEvent(onload, q1a1);
} else{
document.addEventListener(load, q1a1, false);
}

function q1answer1() {

//DO SOME STUFF
}


This also needs to work in as many versions of IE as possible. For single class handling I'm using querySelectorAll.


More From » listener

 Answers
7

What you are really looking for is JavaScript Event Delegation. In your case, you have BUTTON elements, which I'm going to assume are <button> tags. Now you want to know when one of those buttons was clicked and then run a function:



if (document.addEventListener) {
document.addEventListener(click, handleClick, false);
}
else if (document.attachEvent) {
document.attachEvent(onclick, handleClick);
}

function handleClick(event) {
event = event || window.event;
event.target = event.target || event.srcElement;

var element = event.target;

// Climb up the document tree from the target of the event
while (element) {
if (element.nodeName === BUTTON && /foo/.test(element.className)) {
// The user clicked on a <button> or clicked on an element inside a <button>
// with a class name called foo
doSomething(element);
break;
}

element = element.parentNode;
}
}

function doSomething(button) {
// do something with button
}


Anywhere on the page that a <button class=foo>...</button> element appears, clicking it, or any HTML tag inside of it, will run the doSomething function.



Update: Since Event Delegation is used, only a single click handler is registered on the document object. If more <button>s are created as a result of an AJAX call, you don't have to register click handlers on those new <button>s since we take advantage of the click event bubbling up from the element the user clicked on to the document object itself.


[#70874] Thursday, May 22, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kaleyv

Total Points: 259
Total Questions: 99
Total Answers: 107

Location: Saint Helena
Member since Tue, Nov 3, 2020
4 Years ago
;