q0987
q0987

Reputation: 35982

jQuery - Why .att doesn't replace the attribute value?

Based on the manual as follows:

http://api.jquery.com/attr/ 
attr( attributeName, value ) 

I should be able to replace the attribute value of a selected element.

             <div id="keynote1Fld" class="row ">
                <label for="keynote1" class="labeloptional">Topic 1</label>
                <div class="collection">
                 <input type="text" class="largetextfield" maxlength="96" size="96" value="" id="keynote1" name="keynote1" />
                </div>
             </div>

I need to replace the class value of label for keynote1 from labeloptional to label.

Here is what have done:

$("label[for=keynote1]").att('class', 'label');

However, the class value for label of keynote1 still is 'labeloptional' after the execution of the above statement.

why?

Thank you

Upvotes: 1

Views: 624

Answers (1)

Jeff Rupert
Jeff Rupert

Reputation: 4840

You need to use attr not att.


However, if you're changing classes, you shouldn't be using attr().

What you should do is use removeClass() and addClass().

$('label').removeClass('oldClass').addClass('newClass');

That way, you won't be overwriting any other classes that might be assigned to a tag.

Upvotes: 7

Related Questions