Sunday, December 3, 2023
 Popular · Latest · Hot · Upcoming
rated 0 times [  119] [ 5]  / answers: 1 / hits: 28453  / 15 Years ago, thu, april 30, 2009, 12:00:00

First, the background:
I'm working in Tapestry 4, so the HTML for any given page is stitched together from various bits and pieces of HTML scattered throughout the application. For the component I'm working on I don't have the <body> tag so I can't give it an onload attribute.

The component has an input element that needs focus when the page loads. Does anyone know a way to set the focus to a file input (or any other text-type input) on page load without access to the body tag?

I've tried inserting script into the body like

document.body.setAttribute('onload', 'setFocus()')

(where setFocus is a function setting the focus to the file input element), but that didn't work. I can't say I was surprised by that though.


As has been stated, I do indeed need to do this with a page component. I ended up adding file-type inputs to the script we use for giving focus to the first editable and visible input on a page. In researching this problem I haven't found any security issues with doing this.

More From » html


This has worked well for me:

function getLastFormElem(){
var fID = document.forms.length -1;
var f = document.forms[fID];
var eID = f.elements.length -1;
return f.elements[eID];

<input name=whatever id=maybesetmaybenot type=text/>
<!-- any other code except more form tags -->


[#99620] Saturday, April 25, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.

Total Points: 550
Total Questions: 94
Total Answers: 98

Location: Senegal
Member since Fri, Aug 21, 2020
3 Years ago
janettejordynm questions
Tue, Nov 24, 20, 00:00, 3 Years ago
Sat, May 23, 20, 00:00, 4 Years ago
Mon, Apr 6, 20, 00:00, 4 Years ago
Tue, Feb 18, 20, 00:00, 4 Years ago