[thelist] Javascript array loop issues?

Jay Turley jayturley at gmail.com
Thu Oct 29 14:32:15 CDT 2009


Yes, as I shamefacedly pointed out in my second email (which I then screwed
up as well), replying from your phone is a sure way to lower your street
cred.

If I were doing this, using the idea that Tris was demonstrating in her
initial email, I would try something along the following lines:

<input id="name" type="text" />
<input id="email" type="text" />

var getErrors = [
 {
  errorKey: 'name',
  errorMsg: 'Please enter your name'
 },
 {
  errorKey: 'email',
  errorMsg: 'Please enter your email'
 }
];

for ( var i=0, j=getErrors.length(); i<j; i++ ) {
 var $formElement = $('#'+getErrors[i].errorKey);
 if ( $formElement.val() == '' ) {
  alert(getErrors[i].errorMsg);
 }
}

or something...

at least that looks reasonably correct in my text editor... ON MY COMPUTER
:(

On Thu, Oct 29, 2009 at 11:07 AM, Christian Heilmann <codepo8 at gmail.com>wrote:

> Jay Turley wrote:
> > I think your problem may be that you are creating an object literal by
> > using the braces {} instead of the brackets [] to create your array
> > elements. Try it like this:
> >
> > var getErrors = [ errorName: errorMsg, ...];
> >
> > Jay
> >
> >
> >
> No, a "associative array" is a misnomer as in JS it is an object. You
> cannot loop over the properties of an object using length, what you need
> to do is using a for...in loop.
>
>
> http://stackoverflow.com/questions/921789/how-to-loop-through-javascript-object-literal-with-objects-as-members
>
>
> --
>
> * * Please support the community that supports you.  * *
> http://evolt.org/help_support_evolt/
>
> For unsubscribe and other options, including the Tip Harvester
> and archives of thelist go to: http://lists.evolt.org
> Workers of the Web, evolt !
>



More information about the thelist mailing list