SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Jun 2004
    Location
    boston
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem trapping nulls in onload script.

    I have the following tidbit of script that runs in the body onload of an ASP.NET page.

    Document.forms errors out every time ctl is null or blank:

    document.forms(...).elements(...) could not be found

    Am I doing something wrong syntactically?

    Thank you!!
    Paul

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <html>
    <head>
    <title>Status Tracker</title>
    <link href="styles.css" type="text/css" rel="stylesheet" />
    <script language="javascript">
    function setFocus()
    {
    ctl = "<%= controlToFocus %>";
    if (ctl != "") {
    if (ctl != null) {
    document.forms("TimeEntry").elements(ctl).focus();
    }
    }
    }
    </script>
    </head>
    <body onload="setFocus();">

  2. #2
    SitePoint Addict xDev's Avatar
    Join Date
    Jul 2003
    Location
    Moncton, New Brunswick, Canada
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by termid0g
    Am I doing something wrong syntactically?
    Try this
    Code:
    document.forms["TimeEntry"].elements[ctl].focus();
    Just think of the form element as an array, and use the same syntax to target its children.

    You could also do this:
    Code:
    document.TimeEntry.elements[ctl].focus();
    // or
    document["TimeEntry"][ctl].focus();
    Something like this maybe for your page:

    HTML Code:
    <script type="text/javascript">
    
    window.onload = function() {  setFocus(); }
    
    function setFocus()
    {
    	// or whatever number is assigned by "controlToFocus"
    	ctl = 0;
    	if (ctl != "" || ctl != null) {
    		document.forms["TimeEntry"].elements[ctl].focus();
    	}
    }
    
    </script>
    
    </head>
    <body>
    <form name="TimeEntry">
    <input type="text" />
    </form>
    If controlToFocus is a string, use this syntax:
    Code:
    document.forms["TimeEntry"][ctl].focus();
    Last edited by xDev; Jun 28, 2004 at 09:01.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •