Bernardo Baalen
Bernardo Baalen

Reputation: 129

set custom attribute and value with jquery

<div class='progress-20'>

I want to alter the progress-20 with other value, I've get the value by custom expression.

Upvotes: 1

Views: 1908

Answers (4)

Adil
Adil

Reputation: 148110

You can use attr() to set the class attribute of the element. You need to select the div for that you can use class selector.

$('.progress-20').attr('class','newclass');

Edit based on comments.

You can use starts with attribute selector if 20 could change

$('[class^=progress-]').attr('class','newclass');

Upvotes: 1

Tats_innit
Tats_innit

Reputation: 34107

Try this Demo: http://jsfiddle.net/Ja2SU/

OR this demo http://jsfiddle.net/bFLju/

*API:

you could use attr or removeClass and then addClass

Gave you few choices hope anyone will fit you need :)

Code

$(function () {
    alert(' before altering => ' + $('div').attr('class'));
    $('div').attr('class', 'foobar');
    alert(' after altering => ' + $('div').attr('class'));
});

or

$(function () {
    $('div').removeAttr('class').addClass('foobar');
});

Upvotes: 0

Rohan Kumar
Rohan Kumar

Reputation: 40639

Try toggleClass() like,

$('div').toggleClass('progress-20 newclass');

Upvotes: 1

Farhad
Farhad

Reputation: 752

$('.progress-20').attr('class','new_value')

Upvotes: 1

Related Questions