Jump to content

Troubles with forms in js file


Rune Thandy

Recommended Posts

I would like to make a js file which sets the focus on an element in a form.But it has to work from different webpages.So in the webpage it should look something like this:<script>set_focus(form1,element1)</script>And in the js file it should look something like this:function set_focus(page_form,page_element){ document.page_form.page_element.focus()}I know from experience that this doesn't work, but I don't know how to get it right.Can somebody help me?

Link to comment
Share on other sites

it will not work if you are placing the first part in the <head> you will either have to place it just before </body>or place this in the <head> instead

<script>onload=function(){  set_focus(form1,element1)}</script>

Link to comment
Share on other sites

It goes something like this:This is the webpage<html><head><script language="javascript"><%@include file="/include/check_shewart_page.js"%>.........</script></head><body><form name="listmenu0" action="....." id="mp" method="post"><table><tr><td><input type="text" id="datefield1" name="datefield1" tabindex="1"></td><td><input type="text" id="datefield2" name="datefield2" tabindex="2"></td></tr><tr><td><input type="text" id="datefield3" name="datefield3" tabindex="3"></td><td><input type="text" id="datefield4" name="datefield4" tabindex="4"></td></tr></table><script>set_focus("listmenu0","datefield1")</script></body></html>The js file:function set_focus(page_form,page_element){document.page_form.page_element.focus()}When I change page_form in listmenu0 and page_element in datefield1 it works.The webpages are longer, but this is essentially what it's about.

Link to comment
Share on other sites

I still can't get it to work like you suggested, but I finally got to a different solution which worked fine for me.I got back to including the js file in the beginning of the webpage and in the js file I got the following code now:document.forms[page_form].elements[page_element].focus()But thanks for trying to help me.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...