/    Sign up×
Community /Pin to ProfileBookmark

function not firing when clicking on content created on the fly

HI,

Im new here so hello all.

I have a function that captures all clicks ( captureClicks() function). It’s not working with content i’ve created using javascript. I’ve created an a tag with createElement ( createcontent() function )but when i click on it the expected behaviour does not occur. All other elements do correctly alert a message back…..has anyone encountered this type of problem?

[code=html]// cross-browser event handling
function addEvent(element, evType, fn, useCapture) {
if (element.addEventListener) {
element.addEventListener(evType, fn, useCapture);
return true;
} else if (element.attachEvent) {
var r = element.attachEvent(‘on’ + evType, fn);
return r;
} else {
element[‘on’ + evType] = fn;
}
}

//add a listener on page load
addEvent(window, ‘load’, addListeners, false);

function addListeners() {
createContent();
captureClicks();

}

function createContent() {
var obj = document.getElementById(‘container’)
var titlea = document.createElement(‘a’);
titlea.className = ‘selectelement’;
titlea.id = ‘li’;
var txt = document.createTextNode(‘yes this is here’);
titlea.appendChild(txt);

obj.parentNode.appendChild(titlea);

}

function captureClicks() {

document.documentElement.onclick = function(e){
e= window.event || e;
var clickedOn = e.target || e.srcElement;
if(clickedOn.nodeType == 3) {
alert(“this is node type 3”);
clickedOn = clickedOn.parentNode;
}
var id = clickedOn.id;
if(id == ”) {
alert(“there is no id here!”);
} else if(id == ‘hello’ || id == ‘container’) {
alert(“the id of this element is ” + id);
}
}
}[/code]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@Angry_Black_ManJan 13.2008 — my [I]guess[/I] is that either the elements dont exist when the listeners are added, or there is a logic issue. stuff addEvent full of alert()'s to see which conditions are being met and when. stick alerts in createContent() as well so you can follow the pattern.
Copy linkTweet thisAlerts:
@mrhooJan 13.2008 — You have it set up to alert if there is NO id or if the id is 'hello' or 'container'-

it doesn't do anything for elements that have other ids, like 'li'.
×

Success!

Help @elduderino spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...