No. of Recommendations: 0
I have a simple little javascript function in a page that is called when the page loads to set the focus to a specified field on the page.

function setFocus(fldID) {document.getElementById[fldID].focus();}

But, I want to use this to set the focus to an <INPUT> tag which, by definition, does not have an "id". It has only a "name".

Can someone tell me how I go about setting the focus to such a field?
Print the post Back To Top
No. of Recommendations: 0
But, I want to use this to set the focus to an <INPUT> tag which, by definition, does not have an "id". It has only a "name".

Can someone tell me how I go about setting the focus to such a field?


Inputs most certainly can have an ID attribute in addition to a name. (http://www.w3schools.com/tags/tag_input.asp) So, you can just use that.

Or, if you want to stick with the name, you can do:

document.forms['yourFormName'].elements['yourInputName'].focus();

dsbrady
Print the post Back To Top
No. of Recommendations: 0
OK - I guess I mis-read things. I was looking at the w3schools website but didn't see it.

So, any idea why an error is being generated when this script executes? I have triple checked that I am passing a correct field "id" to the script but I get:

TypeError: document.getElementById[fldID] is undefined.
Print the post Back To Top
No. of Recommendations: 0
BTW, here's the page I was looking at. Notice it does not show "id" as a standard property.

http://www.w3schools.com/jsref/dom_obj_text.asp
Print the post Back To Top
No. of Recommendations: 0
You don't need javascript for that, use HTML's autofocus

<input type="text" name="long" id="long" value="" autofocus="autofocus">

Denny Schlesinger
Print the post Back To Top
No. of Recommendations: 0
Thanks, Denny, but from what I read on that, it doesn't work in IE.
Print the post Back To Top
No. of Recommendations: 0
Add an alert to find out:

function setFocus(fldID) {

alert (fldID);
document.getElementById[fldID].focus();

}


Denny Schlesinger
Print the post Back To Top
No. of Recommendations: 0
Denny said: Add an alert to find out:

Yep - I did that. In fact I went even further. I added two other alerts to display the element's id and it's name, like so:

function setFocus(fldID) {

alert(fldID);
alert('id:'+document.getElementById(fldID).id);
alert('name:'+document.getElementById(fldID).name;
document.getElementById[fldID].focus();

}

All three alerts show the correct value! And yet, the .focus statement still generates the error.

I've been fussing around with this for several hours now and simply cannot figure it out. :(

And, BTW, I ran the page through the W3C page validation service - it shows no errors. Any other suggestions will be greatly appreciated.
Print the post Back To Top
No. of Recommendations: 0
I gave it a try and I get the same error in Safari. Stumped! :(

Denny Schlesinger
Print the post Back To Top
No. of Recommendations: 0
BTW, here's the page I was looking at. Notice it does not show "id" as a standard property.

At the end of the list, it says it also supports the standard properties (which links to a list that includes ID). It would have been better to list them all.

The page I found on w3schools actually listed the input-specific properties and the standard ones, so it was clearer.

dsbrady
Print the post Back To Top
No. of Recommendations: 0
You don't need javascript for that, use HTML's autofocus

Given that IE doesn't support that, it's probably best to stick with javascript at this point.

dsbrady
Print the post Back To Top
No. of Recommendations: 0
Are you able to provide a link to the page you're doing this on [I don't think you have yet]? Maybe we'll see something wrong by seeing the whole thing.

Oh, and make sure that the ID you're using is unique. No other elements can have that ID for your code to work.

dsbrady
Print the post Back To Top
No. of Recommendations: 0
Given that IE doesn't support that, it's probably best to stick with javascript at this point.

Or to drop IE! LOL

Denny Schlesinger
Print the post Back To Top
No. of Recommendations: 0
Well, I got it fixed.

I was having a completely unrelated problem that I finally narrowed down to this same GetElementById issue. In this 2nd case, I was trying to get the status of a checkbox (using vbscript). Part of the statement was: "document.getElementById.ckbxABC.checked". This generated the same kind of error.

The fix for all of this that I stumbled across was to replace "GetElementById" with "all". So "document.getElementById[fldID].focus" became "document.all[fldID].focus". And, "document.getElementById.ckbxABC.checked" became "document.all.ckbxABC.checked". When coded this way, everything works just fine.

I can't explain it - but it works so I'm going to go with it.

BTW, dsbrady, this is all part of an in-house setup that is not available for access over the internet.
Print the post Back To Top
No. of Recommendations: 5
First, try to avoid using document.all if you can. It's not really "proper" javascript (in that it was initially IE only and may not be supported by all browsers).

But, now that I'm looking more closely at your initial code, here's the problem:

You had this:
document.getElementById[fldID]
document.getElementById.ckbxABC

getElementById is a function, so it needs to be this:
document.getElementById(fldID)
document.getElementById('ckbxABC');

Try that and it should work in any browser since IE5 on.

dsbrady
Print the post Back To Top
No. of Recommendations: 0
I can't explain it - but it works so I'm going to go with it.

Works for me too. Thanks for posting the solution.

Denny Schlesinger
Print the post Back To Top
No. of Recommendations: 0
dsbrady: WOW! Thank you, thank you, thank you!

Replacing the brackets "[]" with parens "()" did, in fact, fix the problem. I don't think I would have ever picked up on that.
Print the post Back To Top
No. of Recommendations: 0
getElementById is a function, so it needs to be this:
document.getElementById(fldID)


Works for me too! Thanks also for the even better solution.

Denny Schlesinger
Print the post Back To Top
Advertisement