tags:

views:

46

answers:

4

I have an external stylesheet that is applying styles to my TD elements. I would like to move those styles to the DIV tags (content wrappers) that are within the TD tags.

The CSS:

td { padding: 5px }
div { }

The HTML:

<td>
    <div>
        Apply the TD styles to this DIV (instead of the TD).
    </div>
</td>

The JavaScript:

$(document).ready(function(){
    $('tr').children('td').each(function(){
        //move the td styles to div
    });
});

How can I achieve this?

A: 

EDIT: Code corrected

$('div').attr('style', $('td').attr('style'));

If you want to move just add this line afterwards

$('td').attr('style', '');

I realize that this looks contrived but I have no other selectors to work with here. Normally you'd want to do this with an id or a class instead of the actual attribute.

Chuck Vose
This doesn't move the style, it copies.
SamStephens
Title says 'copy' though
stagas
I've rephrased the question to better reflect what I am trying to ask.
Andrew
+1  A: 

EDIT: This answer was written before the question was clarified.

Give your elements IDs, like this:

<td id="td_element" style="padding:5px">
    <div id="div_element">
        Apply the TD styles to this DIV (instead of the TD).
    </div>
</td>

And use

var td_element = $('#td_element')
$('#div_element').attr('style', td_element.attr('style'));
td_element.removeAttr('style');

Of course you could use a class instead - or you may have the elements from previous javascript code. You'll need to decide the best way to reference the elements yourself.

SamStephens
will this work if the styles are being applied from an external stylesheet?
Andrew
No it doesn't, as that's not what you asked for. As you can see from this example, applying the styles is easy. However working out the styles applied to an element including from stylesheets is rather tricky. See this question: http://stackoverflow.com/questions/395341/get-element-stylesheet-style-in-javascript. I'd take the advice contained in the accepted answer for that post: "Perhaps you would do better to step back and ask why you would ever need such a thing?"
SamStephens
@Sam and @Andrew if the styles were defined through classes that were net restricted to `td` elements like `td.someclass{..}` then you could remove the class from the `td` and apply it to the `div`. Look at [addClass()](http://api.jquery.com/addClass/) and [removeClass()](http://api.jquery.com/removeClass/)
Gaby
I've rephrased the question to better reflect what I am trying to ask.
Andrew
A: 

And a more generalized solution from the provided answers..

HTML

<td style="padding:5px">
    <div class="inherit">
        Apply the TD styles to this DIV (instead of the TD).
    </div>
</td>

JS

$('.inherit')
     .attr('style', $(this)
                    .parents('td')
                    .attr('style')
          )
     .parents('td')
     .removeAttr('style');
Gaby
I've rephrased the question to better reflect what I am trying to ask.
Andrew
@Andrew, have a look at this http://www.hunlock.com/blogs/Totally_Pwn_CSS_with_Javascript but i do believe you should just change your css accordingly
Gaby
A: 

Don't move the styles is my short answer - find another way. Can you load another stylesheet after the stylesheet being loaded? If so, add a stylesheet that overrides the td and div padding defined in the stylesheet you're having to reference.

SamStephens