Reputation: 17
I have a problem. I want to set some attributes with JS.
for(i=0;i<div_navi.childNodes.length;i++){
if(div_navi.childNodes[i].nodeName =="SPAN"){
div_navi.childNodes[i].setAttribute("onclick","getContent(div_navi.childNodes[i].textContent);
div_navi.childNodes[i].style.cursor ="pointer";
}
if(div_navi.childNodes[i].nodeName =="A")
div_navi.childNodes[i].setAttribute("href",div_navi.childNodes[i].textContent);
}
}
I want to set a parameter in getContent, but it doesn't work. Has anybody an idea, how this could work?
Upvotes: 0
Views: 90
Reputation: 66355
You don't set an event with setAttribute
, you would assign a function to the onclick
(div_navi.childNodes[i].onclick = [function]
) property, or better yet:
div_navi.childNodes[i].addEventListener('click', getContent);
function getContent(e) {
console.log(e.target.textContent);
}
Upvotes: 0
Reputation: 133403
Use quotes properly and you should bind event properly and you can set href
property instead directly.
div_navi.childNodes[i].onclick = function(){
getContent(this.textContent)
};
div_navi.childNodes[i].href = div_navi.childNodes[i].textContent;
Upvotes: 1