user image

Mohamed Atef
Published in : 2022-01-27

How to get the style of element in jQuery? add it to another

jQuery

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.

Comments

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

Mohamed Atef Date : 2022-01-27

Thank you Joseph, this logic makes sense & worked for me 

Leave a comment

Join us

Join our community and get the chance to solve your code issues & share your opinion with us

Sign up Now

Related posts

Display JSON in HTML using jQuery
Publish date: 2022-01-30 | Comments: 2

Tag: jQuery

How to use !important using jQuery css function?
Publish date: 2022-01-27 | Comments: 2

Tag: jQuery

jQuery Selector is not working .click
Publish date: 2022-02-21 | Comments: 1

Tag: jQuery

jQuery alter the class of a button when I click on it?
Publish date: 2022-02-12 | Comments: 1

Tag: jQuery

[solved] SyntaxError: Unexpected token o in JSON at position 1 jQuery
Publish date: 2022-01-30 | Comments: 2

Tag: jQuery

Accessing Radio button values using Jquery
Publish date: 2022-03-05 | Comments: 1

Tag: jQuery

[SOLVED] jQuery .click() not working with auto added buttons
Publish date: 2021-06-19 | Comments: 2

Tag: jQuery