Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Fix your selector to actually grab only the items which match your conditions:</p> <pre><code>$(".stylesheets[data-css]").data("css").split(" "); </code></pre> <p>You can <a href="http://api.jquery.com/category/selectors/" rel="nofollow">read more about jQuery's selectors here</a>.</p> <p>EDIT</p> <p>As your concern here is with the non-existence of the selector, and because <code>split</code> is a normal Javascript method and lacks some of the "safety" mechanisms the jQuery methods typically come with when dealing with empty collections, this would probably be the most terse way to go:</p> <pre><code>var cssAttr = $(".stylesheet").data("css"); var stylesheets = (cssAttr) ? cssAttr : null; </code></pre> <p>Note that I've only decided to go to the DOM once here by using the <code>cssAttr</code> variable. I would recommend you doing the same here to prevent touching the DOM twice for the same thing.</p> <p>Now, if you really want to optimize, and you don't intend to have multiple matching selectors (and that does appear to be the case based on your code), you could always skip the jQuery here. You're not really doing anything vanilla JS couldn't already do, jQuery isn't really bringing anything to the table, and you'll benefit from a slightly faster selector here:</p> <pre><code>var cssAttr = document.getElementsByClassName("stylesheet")[0].dataset.css; var stylesheets = (cssAttr) ? cssAttr : null; </code></pre> <p>Note you'll need to be targeting <a href="https://developer.mozilla.org/en-US/docs/Web/API/document.getElementsByClassName" rel="nofollow">IE9 and above</a> for that selector to work.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload