Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use selector library, for example Sizzle: <a href="http://sizzlejs.com/" rel="nofollow noreferrer">http://sizzlejs.com/</a> but if you want pure JS that I guess you are stuck with getting all the elements, and then programatically "handpicking" the ones that have classes you are interested in using RegEx like this for example:</p> <p><strong>This is an equivalent of your JQuery oneliner:</strong></p> <pre><code>for( i in document.all) document.all[i].className &amp;&amp; /\bpost-text\b/g.test(document.all[i].className) &amp;&amp; (document.all[i].style.color = "red") </code></pre> <p><em>:)</em></p> <p>If you don't need it in one line you can make it faster (<strong>and much more readable</strong>): </p> <pre><code>var myClassName = "someClass"; var regexp = RegExp("\\b"+myClassName+"\\b/g"); var elements = document.all; for( i in elements){ var this_element = elements[i]; if(regexp.test(this_element.className){ this_element.style.color = "red"; } } </code></pre> <p>If "<strong>for( i in object)</strong>" doesn't work for you, just use classic for loop "<strong>for(var i = 0; i &lt; elements.length; i++)</strong>". </p> <p>It could be '<em>beautified</em>' a bit with the use of some slightly more advanced JS concepts (<em>array function mappings, folding and such</em>), which JS version are you coding agains? I guess it's not ECMA Script 5, right?</p> <p>Also, check out this question/answer <a href="https://stackoverflow.com/questions/210377/get-all-elements-in-an-html-document-with-a-specific-css-class">Get All Elements in an HTML document with a specific CSS Class</a></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.
    1. VO
      singulars
      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