0

I'm trying to search the net, but no result... Simple; how can I get other tag's css and add it to other on click? For example:

I have b tag with css. I need to copy it's css to a tag on click.

var css = $('b').css();

$('.switch > a').click(function() {
    $(this).css(css);
});

Is it something like this?

aksu
  • 5,221
  • 5
  • 24
  • 39
  • yes, but it didn't work – aksu Dec 02 '13 at 16:46
  • 1
    possible duplicate of [jQuery CSS plugin that returns computed style of element to pseudo clone that element](http://stackoverflow.com/questions/1004475/jquery-css-plugin-that-returns-computed-style-of-element-to-pseudo-clone-that-el) – Pranav 웃 Dec 02 '13 at 16:53

3 Answers3

0

use cssText to get and set all the current styles

var css = $('b').css('cssText');

$('.switch > a').click(function(e) {
    e.preventDefault();
    $(this).css('cssText', css);
});

FIDDLE

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • Well, code creates style tag but it won't put anything in it. Could it be because of my old jquery version, 1.5.2? – aksu Dec 02 '13 at 17:19
  • @aksu - Yes, this trick requires jQuery 1.8 or above, as those versions will "polyfill" cssText with the currentStyles etc. – adeneo Dec 02 '13 at 17:23
0

If you know which class is on your id, just do something like that

$('#YourId').attr('class', 'yourCssClass');

If you don't know it's class:

var classA = $('#YourId').attr('class');
$('#YourIdB').attr('class', classA);
0
var newclass= $('b').attr('class');

$('.switch > a').click(function() {
    $(this).attr('class','newclass');
});
Prince Singh
  • 5,023
  • 5
  • 28
  • 32