0

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

xain
  • 13,159
  • 17
  • 75
  • 119

2 Answers2

3

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
  • 217,595
  • 99
  • 455
  • 496
  • 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 Apr 13 '10 at 19:07
  • I'm pretty sure empty() will work for null. Try `throw $break` to break out of the each() loop. – John Conde Apr 13 '10 at 19:21
0

It works me.

example code:

document.observe("dom:loaded", function() {
    var maxHeight = 0;

    $$('.product-name').each(function(i){

        var eleHeight = i.getHeight();

        if (eleHeight > maxHeight){
            maxHeight = eleHeight;
        }
    });

    $$('.product-name').each(function(i){
        i.setStyle({
            height: maxHeight+'px'
        });
    });
});
jruzafa
  • 4,156
  • 1
  • 24
  • 26