View Full Version : javascript getElementByBloodyNameOrFrikinID() ?

09-11-2006, 11:11 AM
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


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

09-12-2006, 12: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

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


should have been on silent hill

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


didnt work

09-12-2006, 01:18 PM
function getStuff(v) {

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

The object is passed, alert its id!

09-12-2006, 01:26 PM
cheers ranjan