gespinha
gespinha

Reputation: 8487

Jquery change the class of a div issue

I am trying to change the class os an element which is icon-local to icon-local-img by adding the -img to the end of the class.

Here is what I am trying. Why is it not working?

HTML

<div class="icon-local">sdvdfvsdfvdfs fdsfggfd</div>

CSS

.icon-local {
    color:#00f;
}
.icon-local-img {
    color:#f00;
}

JS

$(document).ready(function () {
    function swapClass($obj) {
        $($obj).toggleClass($obj + ' ' + $obj + '-img');
    }
    if (!$('html').hasClass('fontface')) {
        swapClass('icon-local');
        swapClass('icon-mail');
        swapClass('icon-phone');
    }
});

http://jsfiddle.net/7Ka5g/3/

Upvotes: 0

Views: 33

Answers (1)

Fiddle Demo

function swapClass($obj) {
    $('.' + $obj).toggleClass($obj + ' ' + $obj + '-img');
     //^ class selector
}

Class Selector (“.class”)

Upvotes: 2

Related Questions