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?

3 Answers 3

1

Here is jsFiddle for your question from another one.

Sign up to request clarification or add additional context in comments.

Comments

1

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

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

1 Comment

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.
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

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.