selectivizr icon indicating copy to clipboard operation
selectivizr copied to clipboard

Apply selectivizr after DOM modifications

Open fcsonline opened this issue 13 years ago • 11 comments

Hello,

First of all thanks for this great tool.

So, my question is if it's possible to apply the CSS after DOM modifications?

An example:

The HTML file:

<table>
    <tr><td>Row 1</td></tr>
    <tr><td>Row 2</td></tr><!-- Appears red -->
    <tr><td>Row 3</td></tr>
    <tr><td>Row 4</td></tr><!-- Appears red -->
</table>

The CSS file:

tr:nth-child(even) td { background:red;}

The javascript code:

$("<tr><td>Row 5</td></tr>").appendTo("table tbody")
$("<tr><td>Row 6</td></tr>").appendTo("table tbody") 

In this example the 'Row 6' dont' appears in color red, because the style of new nodes is not applied.

Is there a solution?

Thanks!

fcsonline avatar Jul 01 '11 09:07 fcsonline

Currently, there is no automatic solution to this but it is something I'm trying to develop but it's not an easy feature to just "bolt on"

You could manually add the selectivizr classes to your new objects before adding them to the DOM. It's not ideal, but it would allow you to move on.

keithclark avatar Jul 04 '11 19:07 keithclark

I'm also needing this right now too. I'm fine to add the classes manually in the mean time. Do you have any documentation on what classes to add?

arronmabrey avatar Jul 10 '11 15:07 arronmabrey

The best way to find that out would be to use IE developer tools to see which classes were added to the elements your modifiying. They all begin with slvzr-.

keithclark avatar Jul 10 '11 16:07 keithclark

And what about to bind DOMNodeInserted Javascript event and filter the selectivizr targets and then apply the appropriate CSS attributes?

fcsonline avatar Jul 10 '11 20:07 fcsonline

Unfortunately, IE6-8 do not support DOMNodeInserted so that's not possible.

keithclark avatar Jul 10 '11 20:07 keithclark

Ouch! , and try to rescan the new nodes manualy. Something like:

$("<tr><td>Row 5</td></tr>").appendTo("table tbody");
$("<tr><td>Row 6</td></tr>").appendTo("table tbody");

$("table tbody").selectivizr(); 
// At this point, all the new nodes in this context have the appropriate CSS attributes

What do you think?

fcsonline avatar Jul 10 '11 20:07 fcsonline

It's actually kind of difficult, as it implies, for example nth-child(3n-2) to keep track of the parent, remove styles to children created and apply the class again just to the ones that match. Not impossible, but requires a bit of thinking

Osoascam avatar Aug 10 '11 15:08 Osoascam

Tracking this issue as this is something that would have been handy at this very moment :-) Not an easy to do thing though…

yatil avatar Oct 14 '11 11:10 yatil

It would be nice to be able to at least re-run Selectivizr on the whole document...

lolmaus avatar Aug 28 '13 14:08 lolmaus

@lolmaus I think #75 could be what you are looking for...

PikachuEXE avatar Sep 13 '13 06:09 PikachuEXE

@PikachuEXE, i've just tried it and it does work. Thank you. ^_^

lolmaus avatar Oct 27 '13 17:10 lolmaus