-1

is it possible to change the class name of css class. for example if i have classA and classB i want to change the class name of classA to classB. the purpose of this is to inherit all the characteristics from classA and classB

http://jsfiddle.net/y8h4qn3L/2/

        .classA{
            position:absolute;
            width:25px;
            height: 25px;
            border-radius: 10px;
            background: #f57df5;
        }
        .classB{

            -webkit-transform : rotate(30deg) translatex(120px);
            background-color:black;
        }

  <div class="classA"></div> 
em33
  • 43
  • 3

2 Answers2

2

Yes you can, but you have some errors in your fiddle code, and in your reasoning.

First of all, you need to load jQuery if you use jQuery features. Secondly, due to a typo the element was not found.

But then the logic. classB add a transformation to the properties of classA, but if you change the class to classB, the element no longer has the properties of classA, so there is nothing to transform.

Long story short, you should not replace the class, but add the class. An element can have multiple classes, so when you give it both classA and classB, it will correctly show the transformed shape:

$(document).ready(function(){
    var x = document.getElementsByClassName('classA');

    // jQuery style
    $(x[0]).addClass('classB');

    // Old Javascript style
    x[0].className += ' classB';  

    // Modern Javascript style, not supported in IE9-
    x[0].classList.add('classB');   

});

Updated fiddle: http://jsfiddle.net/y8h4qn3L/9/

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
0

You can just simply use both classes` names:

<div class="classA classB"></div>

Or in CSS, inherit from one class to another:

    .classA,
    .classB
    {
        position:absolute;
        width:25px;
        height: 25px;
        border-radius: 10px;
        background: #f57df5;
    }
    .classB
    {

        -webkit-transform : rotate(30deg) translatex(120px);
        background-color:black;
    }

   <div class="classB"></div>
MTP
  • 161
  • 11