[jQuery] How to maintain style in a clone?

Please look at this simple test:

I’m trying to log the color of the clone that I made, from an element that had the color blue. But on the log, the color is null it seems.

Why is that? I want to clone an element, maintaining it’s css class. How do I achieve that?

The element is not in the DOM yet, so there is no css for it. If you put it into the page, it will work:

$(document).ready(function() {
  var element = $(".testStyle").clone();
  $("body").append(element);
  console.log("color > " + element.css("color"));
});
2 Likes

I see, that makes sense, thank you very much!