tags:

views:

61

answers:

2

Hi

I need jQuery to add a class to any form element that has no class allready. I made up a function, the hasNoClass.

Is it possible to do in the real life? Pseudo code: add class to "classless" form tag jQuery("form").hasNoClass().addClass("gotClass");

Alternatively it can be done by a "add class to 3rd form start tag on page. No clue how that would be either, but there may be a position or count function Pseudo code: add class to third form tag: jQuery("form").postition("3").addClass("gotClass");

Wow! That was two questions! I only need the one but answer on both would be appreciated just for the sake of knowing and maybe future use.

Br. Anders

+2  A: 

This should do:

$('*:not([class])')

You'd then be able to do

$('*:not([class])').addClass('gotClass');

or

$('*:not([class])').eq(3).addClass('gotClass');

To clarify: the selector matches all elements, *, that are not within the scope: "any element with the attribute, [,], class"

David Hedlund
Bingo! Thanks for a very fast answer
Tillebeck
index - starts from 0. 3rd element will have index 2.
thephpdeveloper
yes, thanks for the remark
David Hedlund
A: 

To get the 3rd form, simply ...

$('form:eq(2)').addClass('gotClass');
thephpdeveloper