Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    May 2005
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    I'm getting error while focusing.

    Hi Everyone,
    I'm a new member to this forum and my question as follows.I'm getting a error in my javascript code while i used to do a delete operation.The code i used in my onload event(inside the body tag) is document.changepassword_form.username.focus() and i'm getting the error as
    document.changepassword_form.username is null or not an object.Any idea abt this pls reply.

  • #2
    Regular Coder
    Join Date
    May 2005
    Location
    Michigan, USA
    Posts
    566
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That means that the JS cannot find the object. Which either means the objects name has changed. Or it is not loaded on before the event occurs!

  • #3
    Senior Coder
    Join Date
    Apr 2005
    Posts
    1,051
    Thanks
    0
    Thanked 0 Times in 0 Posts
    try

    var myElement = document.getElementById('elementIDValue');
    var myElement.focus();

    where elementIDValue = the id value for the form element you wish to set focus to.
    public string ConjunctionJunction(string words, string phrases, string clauses)
    {
    return (String)(words + phrases + clauses);
    }
    <--- Was I Helpfull? Let me know ---<

  • #4
    New Coder
    Join Date
    May 2005
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need a solution for this.

    Quote Originally Posted by SpirtOfGrandeur
    That means that the JS cannot find the object. Which either means the objects name has changed. Or it is not loaded on before the event occurs!
    Hi,
    Thanks for ur kind reply.I understood something from ur reply, but give me a further solution to solve this problem.Bcos I'm a beginner to JS so provide me with a code solution for this.
    Thanks & Regards,
    Srikanth S

  • #5
    Senior Coder
    Join Date
    Apr 2005
    Posts
    1,051
    Thanks
    0
    Thanked 0 Times in 0 Posts
    well, asking for a code solution is probably not the best way to go about getting it.

    maybe if you post a solution you have attempted we can help (the html with the corresponding JS code).

    however, here is an example of what you want.


    JavaScript
    Code:
    function setFocus(id){
      var myElement = document.getElementById(id)
      myElement.focus();
    }
    HTML
    Code:
    <body onload="setFocus('username')">
    <form>
    <input type="text" name="username" id="username" maxlength="128">
    .......
    .......
    .......
    </form>
    where
    .......
    .......
    .......
    represents the rest of your form code.
    [/code]
    public string ConjunctionJunction(string words, string phrases, string clauses)
    {
    return (String)(words + phrases + clauses);
    }
    <--- Was I Helpfull? Let me know ---<

  • #6
    New Coder
    Join Date
    May 2005
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by rlemon
    well, asking for a code solution is probably not the best way to go about getting it.

    maybe if you post a solution you have attempted we can help (the html with the corresponding JS code).

    however, here is an example of what you want.


    JavaScript
    Code:
    function setFocus(id){
      var myElement = document.getElementById(id)
      myElement.focus();
    }
    HTML
    Code:
    <body onload="setFocus('username')">
    <form>
    <input type="text" name="username" id="username" maxlength="128">
    .......
    .......
    .......
    </form>
    where
    .......
    .......
    .......
    represents the rest of your form code.
    [/code]

    Hi,
    Thanks for ur kind reply.Well u asked abt my previous trials to solve my issue, they are as follows

    1.First I tried like this inside the head tag.
    Code:
    onload=function()
    	{
    		document.changepassword_form.username.focus()
    	}
    2.My second trial was next to my </form> tag.
    Code:
    <script type="text/javascript">
    	document.changepassword_form.username.focus();
          </script>
    I finally tried ur solution unfortunately it also produces the same result.
    Any idea abt this pls reply.
    Thanks & Regards,
    Srikanth S

  • #7
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    Is your form named changepassword_form and username field named username? You may need to post the HTML too.

  • #8
    New Coder
    Join Date
    May 2005
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,
    Here is my HTML code for ur reference.
    Code:
    <form name="changepassword_form" method="post" action="useraccountAPI.sgp" onSubmit="Javascript: return confirmDelete();">
    ---------------------------------
    ---------------------------------
    <input type="text" class="sgTextBox" name="username" id="username" style="width:150; height:20;" autocomplete="off" maxlength="16"/ tabindex="1">
    --------------------------
    --------------------------
    </form>
    My onload event is in another file.Is it enough or u need some more pls reply.
    Thanks & Regards,
    Srikanth S

  • #9
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    Maybe the path to the external js file is wrong?

  • #10
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    I was having a problem similar earlier today. I discovered if the element wasn't created yet, then the script wouldn't run. A question I had was when does an onload in the body tag run? After the page loads completely or sometime during the page load?

    With that said try this:
    Code:
    <script type="text/javascript">
    <!--
    function focusbox(){
    document.changepassword_form.username.focus();
    }
    //-->
    </script>
    Put that in your head tags.
    In your body tag add onload="focusbox()"
    Code:
    <body onload="focusbox()">
    So it looks like this which does work.
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    <script type="text/javascript">
    <!--
    function focusbox(){
    document.changepassword_form.username.focus();
    }
    //-->
    </script>
    </head>
    
    <body onload="focusbox()">
    <form name="changepassword_form" method="post" action="useraccountAPI.sgp" onSubmit="javascript: return confirmDelete();">
    ---------------------------------
    ---------------------------------
    <input type="text" class="sgTextBox" name="username" id="username" style="width:150; height:20;" autocomplete="off" maxlength="16"/ tabindex="1">
    --------------------------
    --------------------------
    </form>
    </body>
    You might want to add units to your width and height in your inline styles

  • #11
    Senior Coder
    Join Date
    Apr 2005
    Posts
    1,051
    Thanks
    0
    Thanked 0 Times in 0 Posts
    why is there a random backslash in your input element.?

    remove that and try my code. I tested my code on my local pc and it works fine. (it's not hard to code).
    public string ConjunctionJunction(string words, string phrases, string clauses)
    {
    return (String)(words + phrases + clauses);
    }
    <--- Was I Helpfull? Let me know ---<

  • #12
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    Quote Originally Posted by rlemon
    why is there a random backslash in your input element.?

    remove that and try my code. I tested my code on my local pc and it works fine. (it's not hard to code).
    The floating slash inside the tag will be ignored by the browser and doesn't affect anything. But of course, it should be removed.

    As I said earlier, the path to the external js might be wrong. Or probably the js file contains <script></script> tags or HTML comments <!-- -->


  •  

    Posting Permissions

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