UnThreaded | Threaded | Whole Thread (18) | Ignore Thread Prev Thread | Next Thread
Author: DoLoop Big red star, 1000 posts Old School Fool Add to my Favorite Fools Ignore this person (you won't see their posts anymore) Number: of 16437  
Subject: HTML / Javascript question Date: 7/31/2012 3:21 PM
Post New | Post Reply | Reply Later | Create Poll . Report this Post | Recommend it!
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
Author: dsbrady Big funky green star, 20000 posts Old School Fool Add to my Favorite Fools Ignore this person (you won't see their posts anymore) Number: 16313 of 16437
Subject: Re: HTML / Javascript question Date: 7/31/2012 3:37 PM
Post New | Post Reply | Reply Later | Create Poll . Report this Post | Recommend it!
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
Author: DoLoop Big red star, 1000 posts Old School Fool Add to my Favorite Fools Ignore this person (you won't see their posts anymore) Number: 16314 of 16437
Subject: Re: HTML / Javascript question Date: 7/31/2012 3:51 PM
Post New | Post Reply | Reply Later | Create Poll . Report this Post | Recommend it!
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
Author: DoLoop Big red star, 1000 posts Old School Fool Add to my Favorite Fools Ignore this person (you won't see their posts anymore) Number: 16315 of 16437
Subject: Re: HTML / Javascript question Date: 7/31/2012 3:54 PM
Post New | Post Reply | Reply Later | Create Poll . Report this Post | Recommend it!
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
Author: captainccs Big funky green star, 20000 posts Top Favorite Fools 10+ Year Anniversary! Old School Fool Add to my Favorite Fools Ignore this person (you won't see their posts anymore) Number: 16316 of 16437
Subject: Re: HTML / Javascript question Date: 7/31/2012 8:23 PM
Post New | Post Reply | Reply Later | Create Poll . Report this Post | Recommend it!
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
Author: DoLoop Big red star, 1000 posts Old School Fool Add to my Favorite Fools Ignore this person (you won't see their posts anymore) Number: 16317 of 16437
Subject: Re: HTML / Javascript question Date: 7/31/2012 9:02 PM
Post New | Post Reply | Reply Later | Create Poll . Report this Post | Recommend it!
Recommendations: 0
Thanks, Denny, but from what I read on that, it doesn't work in IE.

Print the post Back To Top
Author: captainccs Big funky green star, 20000 posts Top Favorite Fools 10+ Year Anniversary! Old School Fool Add to my Favorite Fools Ignore this person (you won't see their posts anymore) Number: 16318 of 16437
Subject: Re: HTML / Javascript question Date: 7/31/2012 9:36 PM
Post New | Post Reply | Reply Later | Create Poll . Report this Post | Recommend it!
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
Author: DoLoop Big red star, 1000 posts Old School Fool Add to my Favorite Fools Ignore this person (you won't see their posts anymore) Number: 16319 of 16437
Subject: Re: HTML / Javascript question Date: 7/31/2012 9:54 PM
Post New | Post Reply | Reply Later | Create Poll . Report this Post | Recommend it!
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
Author: captainccs Big funky green star, 20000 posts Top Favorite Fools 10+ Year Anniversary! Old School Fool Add to my Favorite Fools Ignore this person (you won't see their posts anymore) Number: 16320 of 16437
Subject: Re: HTML / Javascript question Date: 7/31/2012 10:44 PM
Post New | Post Reply | Reply Later | Create Poll . Report this Post | Recommend it!
Recommendations: 0
I gave it a try and I get the same error in Safari. Stumped! :(

Denny Schlesinger

Print the post Back To Top
Author: dsbrady Big funky green star, 20000 posts Old School Fool Add to my Favorite Fools Ignore this person (you won't see their posts anymore) Number: 16321 of 16437
Subject: Re: HTML / Javascript question Date: 8/1/2012 12:05 PM
Post New | Post Reply | Reply Later | Create Poll . Report this Post | Recommend it!
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
Author: dsbrady Big funky green star, 20000 posts Old School Fool Add to my Favorite Fools Ignore this person (you won't see their posts anymore) Number: 16322 of 16437
Subject: Re: HTML / Javascript question Date: 8/1/2012 12:06 PM
Post New | Post Reply | Reply Later | Create Poll . Report this Post | Recommend it!
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
Author: dsbrady Big funky green star, 20000 posts Old School Fool Add to my Favorite Fools Ignore this person (you won't see their posts anymore) Number: 16323 of 16437
Subject: Re: HTML / Javascript question Date: 8/1/2012 12:09 PM
Post New | Post Reply | Reply Later | Create Poll . Report this Post | Recommend it!
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
Author: captainccs Big funky green star, 20000 posts Top Favorite Fools 10+ Year Anniversary! Old School Fool Add to my Favorite Fools Ignore this person (you won't see their posts anymore) Number: 16324 of 16437
Subject: Re: HTML / Javascript question Date: 8/1/2012 1:13 PM
Post New | Post Reply | Reply Later | Create Poll . Report this Post | Recommend it!
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
Author: DoLoop Big red star, 1000 posts Old School Fool Add to my Favorite Fools Ignore this person (you won't see their posts anymore) Number: 16325 of 16437
Subject: Re: HTML / Javascript question Date: 8/1/2012 2:30 PM
Post New | Post Reply | Reply Later | Create Poll . Report this Post | Recommend it!
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
Author: dsbrady Big funky green star, 20000 posts Old School Fool Add to my Favorite Fools Ignore this person (you won't see their posts anymore) Number: 16326 of 16437
Subject: Re: HTML / Javascript question Date: 8/1/2012 2:43 PM
Post New | Post Reply | Reply Later | Create Poll . Report this Post | Recommend it!
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
Author: captainccs Big funky green star, 20000 posts Top Favorite Fools 10+ Year Anniversary! Old School Fool Add to my Favorite Fools Ignore this person (you won't see their posts anymore) Number: 16327 of 16437
Subject: Re: HTML / Javascript question Date: 8/1/2012 3:19 PM
Post New | Post Reply | Reply Later | Create Poll . Report this Post | Recommend it!
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
Author: DoLoop Big red star, 1000 posts Old School Fool Add to my Favorite Fools Ignore this person (you won't see their posts anymore) Number: 16328 of 16437
Subject: Re: HTML / Javascript question Date: 8/1/2012 3:20 PM
Post New | Post Reply | Reply Later | Create Poll . Report this Post | Recommend it!
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
Author: captainccs Big funky green star, 20000 posts Top Favorite Fools 10+ Year Anniversary! Old School Fool Add to my Favorite Fools Ignore this person (you won't see their posts anymore) Number: 16329 of 16437
Subject: Re: HTML / Javascript question Date: 8/1/2012 3:23 PM
Post New | Post Reply | Reply Later | Create Poll . Report this Post | Recommend it!
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
UnThreaded | Threaded | Whole Thread (18) | Ignore Thread Prev Thread | Next Thread
Advertisement