How about something like:

$required = $(".required"); // list of elements with 'required' class.
$.each($required, function(i) {
     var id = $(this).attr('id');
     var $label = $("label[for="+id+"]");
     $label.text( $label.text()+'*' );
});

The requirement is that all your .required input's ID should have a
corresponding label with a matching 'for' ID. (It's not going to bomb
or anything if it doesn't.)


On Mar 31, 3:46 pm, Baum <dbau56...@gmail.com> wrote:
> One note the javascript included in the original post is incorrect...I
> was playing around and pasted the wrong copy...remove the each(). Also
> var size = $(".required").size(); is equal to 1 but var size = $
> ("input").size(); is equal to 2.
>
> Thanks.
>
> On Mar 31, 7:56 pm, Baum <dbau56...@gmail.com> wrote:
>
> > Hi,
>
> > I am looking to use jQuery in conjunction with xVal for validation.
> > One of the cool things I though jQuery would allow me to do is append
> > an asterisk (*) to the label of "ALL" my fields that have a css class
> > of "required". The example below only applies the asterisk to the
> > first field.
>
> > How can I get all of the fields from my page that have a
> > class="required", find the label (previous element) for each, and
> > append some text (an asterisk) to the label for that field?
>
> > Example
> > ======================
>
> > <script type="text/javascript">
> > $(document).ready(function() {
> >            //$('label.required').append('&nbsp;<strong>*</
> > strong>&nbsp;');
> >            $(".required").each().prev().not('input').not('br').not
> > ('select').append(' * ');
> >        });
> > </script>
>
> > <label for="FirstName'>LastName</label>
> > <input id="FirstName" class="required" />
>
> > <label for="LastName">LastName</label>
> > <input id="LastName" class="required" />
>
> > Thanks,
>
> > Baum
>
>

Reply via email to