Rune Thandy Posted August 31, 2006 Share Posted August 31, 2006 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 More sharing options...
aspnetguy Posted August 31, 2006 Share Posted August 31, 2006 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 More sharing options...
Rune Thandy Posted August 31, 2006 Author Share Posted August 31, 2006 I know that bit about putting the function in the webpage at the end.My problem is: the function in the js file doesn't work and I don't know how to get that right. Link to comment Share on other sites More sharing options...
aspnetguy Posted August 31, 2006 Share Posted August 31, 2006 you are including the link to the external js file right? Link to comment Share on other sites More sharing options...
Rune Thandy Posted August 31, 2006 Author Share Posted August 31, 2006 Yes, the link works fine.When I put in the right name of the form and element it works, but when I try to make it dynamic it goes wrong. Link to comment Share on other sites More sharing options...
aspnetguy Posted August 31, 2006 Share Posted August 31, 2006 okay then can you post the full contents of the external js file and an full html page using this js file (that si not working) so I am can reproduce the situation. Link to comment Share on other sites More sharing options...
Rune Thandy Posted August 31, 2006 Author Share Posted August 31, 2006 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 More sharing options...
aspnetguy Posted August 31, 2006 Share Posted August 31, 2006 try this instead <script language="javascript" src="/include/check_shewart_page.js"></script><script language="javascript">.........</script> Link to comment Share on other sites More sharing options...
Rune Thandy Posted August 31, 2006 Author Share Posted August 31, 2006 It gives an error when it loads the page now."Object expected"On the line: set_focus(....) in the webpage. Link to comment Share on other sites More sharing options...
aspnetguy Posted August 31, 2006 Share Posted August 31, 2006 I should have noticed thatchange the js file to look like this function set_focus(page_form,page_element){ document.getElementById(page_element).focus()} Link to comment Share on other sites More sharing options...
Rune Thandy Posted September 1, 2006 Author Share Posted September 1, 2006 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now