Mohamed Atef
Published in : 2022-01-27
Hello everyone, I am wondering How can I get a style attr of a specific element using jQuery and add it to another div? for example if I have element like
<div >...</div>
How can I add the same style to another div with class posts-container-custom?
Thanks.
Joseph Morgan Date : 2022-01-27
Best answers
11
Best answers
11
Hi Mo, you can achieve what you need using this code to get the style attribute
var existsStyle = jQuery("div.posts-container").attr('style');
console.log(existsStyle);
then you can add the style to the second div using
jQuery("div.posts-container-custom").attr("style", existsStyle);
so the entire code should looks like
var existsStyle = jQuery("div.posts-container").attr('style');
console.log(existsStyle);
jQuery("div.posts-container-custom").attr("style", existsStyle);
Good luck
Join our community and get the chance to solve your code issues & share your opinion with us
Sign up Now