My naive approach is the following:
function isClickable(id){
elem = document.getElementById(id);
if (elem.nodeName.toLowerCase() == 'a' || typeof(elem.click) != 'undefined'){
return true;
}else{
return false;
}
}
Is there anything better I can do?
For most elements...
if(e.getAttribute('onclick')!=null){
// clickable
}
For anchors...
if(e.getAttribute('href')!=null){
// clickable
}
Then you have form buttons which require a little more code, and finally you have click-event bubbling to deal with so a perfect solution covering ALL elements would be a nightmare!
However, if you just want something SIMPLE for containers and anchors only, then we can combine the logic above with a...
if((e.getAttribute('onclick')!=null)||(e.getAttribute('href')!=null)){
// clickable
}
For the reverse...
if((e.getAttribute('onclick')===null)&&(e.getAttribute('href')===null)){
// not clickable
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With