View Full Version : Getting ID of element that calls the function
jimmygoon
11-24-2005, 03:15 PM
I need to figure out the id of the element that calls a particular function.
how can I do this?
liorean
11-24-2005, 03:27 PM
If the function is a member of the element in question, you simply have to read the value of this.id.
jimmygoon
11-24-2005, 05:55 PM
If the function is a member of the element in question, you simply have to read the value of this.id.
I don't think it is because I tried that.
I'm calling this function with an onclick with an anchor tag.
So... can I do this?
liorean
11-24-2005, 05:58 PM
Pass along the this object as an argument to the function. Then in the function you read the id property of that argument.
jimmygoon
11-24-2005, 08:41 PM
Pass along the this object as an argument to the function. Then in the function you read the id property of that argument.
not worth it.
for what its worth if I'm just adding another argument in I'll just pass the Id of it ... that allows for some more flexibility anyways
thanks though
You may capture the event. For instance if the event which triggers your function is onclick:
<script type="text/javascript">
document.onclick=check;
var myid;
function check(e){
var target = (e && e.target) || (event && event.srcElement);
if(target.id){myid=target.id}
}
function blah(){
alert(myid)
}
</script>
But it is not clear to me. You want to capture and to use the id during the execution of that function? Or you want to have a list with the elements which might trigger that function on some (possible different) events?
and here's a method to build an array with the ids of elements which might trigger the function called myfunction() by the event myevent;
<script type="text/javascript">
var myfuncname='myfunction()';
var myevent='onclick'
var myids=[];
onload=function(){
var el = document.getElementsByTagName('*');
for(var i=0;i<el.length;i++){
var a = el[i].getAttributeNode(myevent);
if(a&&typeof a.nodeValue!='object'&&a.nodeValue.indexOf(myfuncname)>-1){
myids[myids.length]=el[i].id;
}
}
alert(myids.toString())
}
</script>
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.