views:

152

answers:

5

I would like to change the class for all the fields in a specific fieldset.

Is there a way to loop through the fields in a fieldset?

A: 

Permanently? Find & replace in your editor of choice.

When the user clicks something? jQuery way:

$('fieldset <selector>').each(function() {
  $(this).removeClass('old').addClass('new');
});
Alec
+1  A: 

Using jQuery (yay!):

$('#fieldset-id :input').each(function(index,element) {
    //element is the specific field:
    $(element).doSomething();
});
Rex M
Where did the OP mention that he/she is using jQuery?
J-P
@J-P just because they didn't explicitly say they aren't using jQuery doesn't mean it's not a viable option. It's overkill to bring in a library to solve this problem, but the next similar problem we save a little bit, and the time after that we save a little more and then it's worth it.
Rex M
Well, why not explore all other possibilities then? MooTools? Prototype? Dojo? Ext? Qooxdoo? ... I think it's best to always assume that the OP wishes to use *no* library, unless explicitly stated.
J-P
+1  A: 

Note the solution below is for NON-JQUERY Implementations.

Implement a getElementsByClassName Method like this:

After you implement the code below you can then use document.getElementsByClassName("elementsInFieldSetClass") it will return an array of the elements with that class.

function initializeGetElementsByClassName ()
        {
            if (document.getElementsByClassName == undefined) {
                document.getElementsByClassName = function(className)
                {
                    var hasClassName = new RegExp("(?:^|\\s)" + className + "(?:$|\\s)");
                    var allElements = document.getElementsByTagName("*");
                    var results = [];

                    var element;
                    for (var i = 0; (element = allElements[i]) != null; i++) {
                        var elementClass = element.className;
                        if (elementClass && elementClass.indexOf(className) != -1 && hasClassName.test(elementClass))
                            results.push(element);
                    }
                    return results;
                }
            }
        }

window.onload = function () {
    initializeGetElementsByClassName();
};
John Hartsock
+2  A: 

You can use getElementsByTagName.

var fieldset= document.getElementById('something');
var fieldtags= ['input', 'textarea', 'select', 'button'];

for (var tagi= fieldtags.length; tagi-->0) {
    var fields= fieldset.getElementsByTagName(fieldtags[tagi]);
    for (var fieldi= fields.length; fieldi-->0;) {

        fields[fieldi].className= 'hello';
    }
}

(If you only care about input fields, you could lose the outer tag loop.)

If you needed them in document order (rather than grouped by tag) you'd have to walk over the elements manually, which will be a pain and a bit slow. You could use fieldset.querySelectorAll('input, textarea, select, button'), but not all browsers support that yet. (In particular, IE6-7 predate it.)

bobince
THANK YOU for offering a viable solution that doesn't employ an entire library to do something so simple. +1
J-P
Question though, what's wrong with simply `fieldi--` ... isn't the `>0` test kind of redundant?
J-P
It's a general idiom for reverse (or order-irrelevant) iteration. It's redundant in this case, but otherwise it would be defensive against the top bound being lower than the bottom bound (in which case you'd get an infinite loop without the `>0`).
bobince
A: 

Another jQuery solution here.

If you are simply adding a class(es) to the elements, it's this simple:

$('fieldset :input').addClass('newClass');

.addClass() (like many other jQuery functions) will work on all of the elements that match the selector.

Example: http://jsfiddle.net/HANSG/8/

Jon Dowdle