SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 28

Thread: class question

Hybrid View

  1. #1
    SitePoint Addict richard_h's Avatar
    Join Date
    May 2002
    Location
    London
    Posts
    301
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    class question

    I'm writing a javascript class for form validation and have a problem moving focus to form elements through the object.

    The focus will move to the form element for a split second before going.

    This won't work when called within the class:
    document.forms[0].elements[2].focus();

    Does anyone know the answer or the reason it does this?

    Many thanks in Advance.

  2. #2
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can't see anything that's wrong with
    document.forms[0].elements[2].focus();

    But this is confusing:
    The focus will move to the form element for a split second before going
    Can you explain what you want to happen?
    The focus will move to the element, but at the same time the form will be posted?

  3. #3
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Presumably you're doing your validation via the onSubmit event - you need to return false to the event, otherwise the form submission will continue.

    Something like this:

    Code:
    <SCRIPT>
    function ValidateForm()
    {
      if (this==that)
      {
        return true;
      }
      else
      {
        document.forms.myfield.focus();
        return false;
      }
    }
    </SCRIPT>
    <BODY onSubmit="return ValidateForm();">
    ...
    ...
    ...
    ...
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  4. #4
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ...for a split second...
    Still confused

    M@rcos "return false"-solution will stop the submission

    Is this what you are looking for?
    document.forms[0].elements[2].focus();
    setTimeout('document.forms[0].submit()', 1000);
    return false;

  5. #5
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    richard_h, what exactly are you trying to do? We don't really have enough information to work with!!!
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  6. #6
    SitePoint Addict richard_h's Avatar
    Join Date
    May 2002
    Location
    London
    Posts
    301
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hopefully this will make things clearer, if you cut and paste this code below you'll see what I mean.

    PHP Code:
    <script language="JavaScript">
        function 
    formValidation()
        {
        
    /*methods*/
        
    this.emailCheck emailCheck;
        
        
    /*messages*/
        
    this.error         'sorry your email address is invalid';
        }
        
        function 
    emailCheck()
        {    
            if(
    document.forms[0].elements[0].value == '')
            {
            
    alert(this.error);
            
    document.forms[0].elements[0].focus();
            
            return 
    false;
            }
            else
            {
            return 
    true;
            }
        }
        
        function 
    instance()
        {
        
    = new formValidation();
        
    v.emailCheck()
        }
        
    </script>
            
    </head>
    <body>
    <form onsubmit="return instance()">
    <input type="text" name="email">
    <input type="Submit" value="submit">
    </form>
    </body>
    </html> 
    I'm writing a validation class, which I can re-use on other projects, but there seems to be an issue when creating your own JavaScript objects, and then trying to manipulate form fields.
    Last edited by richard_h; Jul 4, 2002 at 00:20.

  7. #7
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    maybe you're not getting a reference handle on it.
    Try alerting the object before trying a method on it.

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  8. #8
    SitePoint Addict richard_h's Avatar
    Join Date
    May 2002
    Location
    London
    Posts
    301
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure if I know what you mean, creating javascript objects is all a bit new to me.

  9. #9
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Lol - You're RIGHT next door to me - just noticed!

    Right:

    Code:
    function emailCheck()
        {    
            if(document.forms[0].elements[0].value == '')
            {
            alert(this.error);
    /*             NOTE              */
            alert(document.forms[0].elements[0]);
            document.forms[0].elements[0].focus();
            
            return false;
            }
            else
            {
            return true;
            }
        }
    That will tell you if you have an object or not.
    if you DO have an object - alert the tagName - then the outerHTML - that way you'll see if you're where you think you are - and if you can actually focus that element.

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  10. #10
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Isn't this much better?

    function instance()
    {
    v = new formValidation();
    return v.emailCheck();
    }

  11. #11
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes - but still doesn't help the problem at hand - for which we need the author's reply.

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  12. #12
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    BTW, I think sending the form to validate as a parameter to the constructor is more OO

    1.
    <form onsubmit="return validateForm(this)">

    2.
    function validateForm(frm)
    {
    var v = new formValidator(frm);
    return v.emailCheck();
    }

    3.
    function formValidator(frm)
    {
    this.frm = frm;
    this.emailCheck = emailCheck;
    }

    4.
    function emailCheck()
    {
    if(this.frm.elements[0].value == '')
    {
    etc etc...
    }
    }

  13. #13
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Flawless_koder
    yes - but still doesn't help the problem at hand - for which we need the author's reply.

    Flawless
    It does help the problem at hand
    Try it!

  14. #14
    SitePoint Addict richard_h's Avatar
    Join Date
    May 2002
    Location
    London
    Posts
    301
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, I live in Croydon but work in East Grinstead (stones through).

    Eveything points to the fact I can place the focus on the form element.

    Jofa's method works, but it still bugs me why I can't do it the original way?

  15. #15
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    * throw :P


    And yeah - it bugs me to.

    can you alert the object's type - so you can check it can be focused!?

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  16. #16
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Jofa - working out of my head here - so can't try it.

    I'll believe you

    Not sure why returning it would alter it's ability to focus - unless it's causing a bubble to get nulled by not having a handle open to the object to focus it ...

    Shame you can't strace on windows

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  17. #17
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Jofa's method works, but it still bugs me why I can't do it the original way?
    All I had to do to make it work was adding the "return" in the function instance()

    The second alternative, sending the form ref as a parameter, is just better coding

  18. #18
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you missed my post catch up

    he he

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  19. #19
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not sure why returning it would alter it's ability to focus
    <form onsubmit="return instance()"> !!!

    If the instance function does not return false, the submission is not stopped, and in this case (no action or method specified) the form is sent to the same page, when this page is loaded, the email text field loses focus
    The default method is GET - look in your browser's address field and you will see "?email=" appended to the address

  20. #20
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I see your point - i didn't read through the code completely and assumed it was an onsubmt="return false;whatever();" and letting whatever submit if it wants to.

    I normally do that in case i want to change attributes which will then want to be re-checked - if you get what i mean.

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  21. #21
    SitePoint Wizard Bill Posters's Avatar
    Join Date
    Dec 2001
    Location
    UK
    Posts
    1,523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Flawless_koder
    * throw :P


    And yeah - it bugs me to.
    * too :P :P

    Sorry Flawless, couldn't resist
    New Plastic Arts: Visual Communication | DesignateOnline

    Mate went to NY and all he got me was this lousy signature

  22. #22
    SitePoint Addict richard_h's Avatar
    Join Date
    May 2002
    Location
    London
    Posts
    301
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for your posts I can now see that with the missing 'return', the instance() function will always return true.
    Which explains everything!

  23. #23
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i just CAN'T believe i gave that one away @ Bill.



    I REALLY should have seen that

    Oh well - you win some you loose some!

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  24. #24
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Flawless_koder
    Oh well - you win some you loose some!
    And just to add insult to injury, that should actually be
    Oh well - you win some you lose some!
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  25. #25
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh god!
    How embarassing!

    I'm normally quite proficient - but right now....
    ummm .. let's just pretend i don't exist!



    THANKS Marco - i know you've been waiting for that for a while

    he he

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---


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
  •