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?
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?
Share Improve this question asked Apr 12, 2011 at 7:50 GuyGuy 14.8k27 gold badges72 silver badges88 bronze badges 8- 3 i mean, technically everything is clickable... – Jason Commented Apr 12, 2011 at 7:57
- You're right. Would it help if I rephrase to: "if I click on it, will something happen?" – Guy Commented Apr 12, 2011 at 8:00
- what are you trying to acplish by determining this? – Jason Commented Apr 12, 2011 at 8:01
- Actually Firefox needs a nasty script to make it possible to click some html elements programatically – mplungjan Commented Apr 12, 2011 at 8:01
- My goal is testing - after I do some action, does an element bee clickable (stops being clickable) – Guy Commented Apr 12, 2011 at 8:04
1 Answer
Reset to default 1For 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 bine 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
}