I was trying to pass the CSS of a element to another element, I wrote a function (demo included):
extendcss = (el1, el2) => {
if(typeof(el1) == 'object' && typeof(el2) == 'object'){
Array.prototype.slice.call(el1.attributes).forEach(function (item) {
el2.setAttribute(item.name, item.value);
});
}
else{
Array.prototype.slice.call(document.querySelector(el1).attributes).forEach(function (item) {
document.querySelector(el2).setAttribute(item.name, item.value);
});
}
}
$('button').click(()=>{
extendcss('#parent', '#child');
});
#parent{
color: white;
background: black;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1 id="parent">Parent</h1>
<h1 id="child">Child</h1>
<button>DO</button>
The problem is that, when the method is called, all the attributes of the first element, like id, class, style, and other things get included and added to second element. I haven't figured out how to pass the style only to the second element.
Please help me in passing only style of the element from the element.