0

Is it possible to write a JQuery function that will remove everything with a specific :before content.

here is an example statement to give you something to show me with code:

remove all elements in the document that have a :before pseudo element with content set to "delete me"

James Donnelly
  • 126,410
  • 34
  • 208
  • 218
Ryan Saxe
  • 17,123
  • 23
  • 80
  • 128
  • You want to remove the entire node, or just its `:before/:after` pseudo-element? – yckart Jul 31 '13 at 08:48
  • Seems like it is problematic: http://stackoverflow.com/questions/5041494/manipulating-css-pseudo-elements-using-jquery-e-g-before-and-after – Ivan Chernykh Jul 31 '13 at 08:49

2 Answers2

1

Mhh, you can try something like this:

var sheets = document.styleSheets;
var len = sheets.length;
var ba = /:before|:after/;

// Loop throught all stylesheets
for (var i = 0; i < len; i++) {

    // work out which method to get the CSS rules
    var sheet = sheets[i],
        rules = sheet.cssRules || sheet.rules;

    // Loop through all rules
    for (var r = 0, rule; rule = rules[r++];) {

        // if this rule uses one of the test selectors
        if (rule.selectorText.match(ba)) {
            if (rule.style.content.indexOf('delete me') !== -1) {
                var elems = document.querySelectorAll(rule.selectorText.split(':')[0]);            
                for (var e = 0, elem; elem = elems[e++];) {
                    // remove the node
                    document.body.removeChild(elem);
                }
            }
        }
    }
}

http://fiddle.jshell.net/6m5kB/1/

yckart
  • 32,460
  • 9
  • 122
  • 129
  • can you animate that before in that js too? – Hushme Jul 31 '13 at 09:05
  • that div content is also not visible? you have removed that div too – Hushme Jul 31 '13 at 09:07
  • As I said, *try something like*. Doesn't mean to use this code as it is! **But** I've updated my answer: http://fiddle.jshell.net/6m5kB/1/ – yckart Jul 31 '13 at 09:13
  • @Hushme What he ask is what I did: *Remove all elements in the document that have a `:before` pseudo element with content set to "delete me"* and that's exact the same what my code does ;) So what's your question now? – yckart Jul 31 '13 at 09:17
0

there is simple answer just add class on that element with before and remove it through that class. pseudo-element does not exist in dom that why you cannot directly remove them through jquery or js

Hushme
  • 3,108
  • 1
  • 20
  • 26