Link to home
Start Free TrialLog in
Avatar of Wakie
WakieFlag for Australia

asked on

Import CSS from one ID to another ID using jQuery

Hi all,

I'm wanting to import an existing ID's style ("box") into a different ID's style ("box2"), however my code below does not work as expected.

Does anyone know if this is possible or can direct me to an appropriate jQuery plugin?

Thanks,
Wakie.
<style type="text/css">
	#box { width: 250px; height: 200px; }
</style>

<script type="text/javascript">
$(function() {
	$('box2').css('#box');
});
</script>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of andrewbleakley
andrewbleakley
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Wakie

ASKER

Thanks Andrew,

I ended up using the solution below.

However, any idea why I have to put it at the bottom of the code and not in with the rest of my JavaScript?
<script>
	$("#title").addClass("ui-widget-header ui-corner-all");
	$("#box").addClass("thebox ui-widget-content ui-corner-all");
</script>

Open in new window

because you have to wait until the elements have been created by the DOM. That's why most jQuery code is wrapped in document ready code - it ensures that all the elements you are about to reference exist and that there properties are set.

Try

$(document).ready(function() {
   
$("#title").addClass("ui-widget-header ui-corner-all");
$("#box").addClass("thebox ui-widget-content ui-corner-all");

});
Avatar of Wakie

ASKER

Great stuff, appreciate the help :)
Always a pleasure, best of luck with rest of the job