0

I would like to know the best way to accomplish the following task: Using jquery or pure javascript code for copy the style of one dom element to another.

example:

// css file
div.myClass {
    color: red;
} 

// html file
<div class="otherClass">
 no style
</div>

// js script
var propertyName = color;
$("div.otherClass").css( propertyName , function () {
    return $("div.otherClass").css(propertyName);
}  )  

The js script works but I would like to take the propertyName automatically. Any idea?

underscore666
  • 1,719
  • 4
  • 24
  • 37

3 Answers3

1

why not append the same class from one to the other?

$("div.otherClass").addClass('myClass');
Joseph
  • 117,725
  • 30
  • 181
  • 234
  • Sorry maybe I was not clear in my question. This way it does not work in my case because renaming the class I will bind some event which I don't want. – underscore666 May 16 '12 at 13:21
1

Here is jsFiddle for your question from another one.

Community
  • 1
  • 1
Nikita Shekhov
  • 493
  • 3
  • 11
0

if you want to replace the style properties of that element then you could also do something like this

$("div.otherClass").removeClass("otherClass").addClass("myClass");

this will first remove the class otherClass and then ad myclass to the element

otherwise Joseph's answer would work best

Sandeep Rajoria
  • 1,243
  • 8
  • 14
  • Sorry maybe I was not clear in my question. This way it does not work in my case because renaming the class I will bind some event which I don't want. – underscore666 May 16 '12 at 13:21