Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
114
rated 0 times [  119] [ 5]  / answers: 1 / hits: 28518  / 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.



EDIT:

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

 Answers
2

This has worked well for me:



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


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

<script>getLastFormElem().focus();</script>

[#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.
janettejordynm

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

Location: Senegal
Member since Fri, Aug 21, 2020
4 Years ago
;