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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Apr 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exiting a .each in json

    Hello,

    Perhaps this is a trivial problem, but I've been searching for quite a while with no success, so I thought I'd ask here.

    In a piece of code of mine, I loop through an array containing the names of several fields on my web page using .each. If any fields are empty, I want it to pop up a message displaying that that field is empty and exit exit the javascript function, but all my attempts are failing. Instead it goes through every object in the array, so it'll popup "auctionID is empty", "password is empty", etc. I tried return;, return 1;, exit, break, etc. (json is relatively new to me, so I'm still learning what's what in it.)

    Anyways, here's some code:

    Code:
            Event.observe("Login", "submit",
                            function(event)
                            {
                        aTextFields = new Array("auctionID", "password");
    
                         aTextFields.each(
                                 function(elem, pos)
                                 {
                                        if (Form.Element.checkForEmpty(elem, $F(elem)))
                                        {
                                            Event.stop(event);
                                            return false;
                                        }
                                 })
                         });
    
        /****************
        * Extend Form.Element with checkForEmpty
        * Purpose: checks the fields, and if they're empty tell the users which ones.
        ****************/
        Object.extend(Form.Element,
            {
                checkForEmpty: function(elem, value)
                {
                        if (value.length == 0)
                        {
                                alert("Element " + elem + " is empty ");
                                $(elem).focus();
                                return true;
                        }
                        return false;
                }
            });
    I wrote this a while back so I'm not as familiar with it as I once way, but it feels like Event.stop(event) should be what prevents the code from continuing from running. Once again if someone could help me, I'd be very grateful. Thanks!

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,298
    Thanks
    10
    Thanked 584 Times in 565 Posts
    there's no way to stop .each early.

    you can use a for loop instead, and the break or return can get you out early...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #3
    New to the CF scene
    Join Date
    Apr 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hmm, too bad. I thought of a quick little hack to get by this: declaring a boolean, and if it enters the if loop set to false. only enter the if loop if the boolean is true. This seems to work.

    Thanks for the information.


  •  

    Posting Permissions

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