views:

17

answers:

1

Hi, I want to iterate over an array of inputs that belong to certain class (eg."required"). How can I traverse it and get their values ? Something like

$$('input required').invoke(function(e){
      alert(?input value?)
    });

thanks

+1  A: 

You're close:

$$('input.required').each(function(i){
    console.log($F(i));
});

All inputs with the class of required will be iterated through and their value displayed to the Firefox console. If you don't use Firefox just change console.log to alert to see the results.

John Conde
John, thanks for your reply, I'm almost there, 2 more things if you'd be so kind; in the code: $$('input.required').each(function(i){ console.log(i.name+':'+$F(i)); if ( i.empty() ) { alert("Must Enter "+i.name); i.focus(); return; } });1) is empty() good enough to check for null and whitespace-only strings?2) return doesn't break the loop, how can I leave the "each" loop ?
xain
I'm pretty sure empty() will work for null. Try `throw $break` to break out of the each() loop.
John Conde
Worked just fine, thanks!
xain