PDA

View Full Version : javascript getElementByBloodyNameOrFrikinID() ?


davidj
09-11-2006, 12:11 PM
ok lets get this over :oops:

i am calling a js function from a large form and rather than pass into the function the exact element name (trying to keep this as generic as poss) i am using the object call -> this

example...

function getStuff(v){

do stuff

}


<input name="branch[]" type="text" class="form1"
onKeyUp="getStuff(this)" id="branch">

as you can see when im firing getStuff() i am passing this which means i can grab the value by using v.value within the function. This is great as one function can be used to format many form elements

Q. How do i find the ID of the element i am in from within the function ????

I know i could pass another argument which identifies the element but that means i will have to add the getStuff(this, "field1"), getStuff(this, "field2"), getStuff(this, "field3") 30 times. It would be better to get the element Name from within the function which means less work

chriskq
09-12-2006, 01:22 AM
does this help....
function returnObjById( id )
{
if (document.getElementById)
var returnVar = document.getElementById(id);
else if (document.all)
var returnVar = document.all[id];
else if (document.layers)
var returnVar = document.layers[id];
return returnVar;
}

sorry only just starting to come to grips with js Dj.

ps - wadda think of my pyscho bunny

davidj
09-12-2006, 08:46 AM
wadda think of my pyscho bunny

nightmare-ish

should have been on silent hill

davidj
09-12-2006, 09:20 AM
already tried that ....

document.getElementById(id);

didnt work

ranjan
09-12-2006, 02:18 PM
function getStuff(v) {
alert(v.id);
}



<input name="branch" type="text" class="form1" onBlur="getStuff(this)" id="branch">

The object is passed, alert its id!

davidj
09-12-2006, 02:26 PM
cheers ranjan