JavaScript preprocessors can help make authoring JavaScript easier and more convenient. For instance, CoffeeScript can help prevent easy-to-make mistakes and offer a cleaner syntax and Babel can bring ECMAScript 6 features to browsers that only support ECMAScript 5.
Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
<h1>Selecting Partial Attribute Names</h1>
<h2>using Xpath</h2>
<p>Selecting elements based on partial attribute name match isn't something CSS or JavaScript are equipped to do, but it <em>is</em> something which XPath is able to select fairly easily!</p>
<p>Below we have two examples of tags with custom attribute names, and if we wanted to select them in CSS or JavaScript we would have no way other than using the CSS selector <code>data-custom-1, data-custom-2</code>. But we can only write selectors like this if we know all of the custom attributes that might show up.</p>
<p>By using XPath with the following selector: <code>//*[@*[starts-with(name(), "data-custom-")]]</code> we are able to select all tags whose attributes begin with <code>data-custom-</code></p>
<h2>Demo:</h2>
<div data-custom-1>custom 1</div>
<div data-custom-2>custom 2</div>
var tags = document.evaluate('//*[@*[starts-with(name(), "data-custom-")]]', document, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null)
try {
var node = tags.iterateNext()
while (node) {
node.style.color = 'black'
node.style.background = 'lime'
node = tags.iterateNext()
}
}
catch (e) {
console.log('couldn\'t style ' + e)
}
Also see: Tab Triggers