Rectangle 27 258

No, it's not possible using just one selector. The :first-of-type pseudo-class selects the first element of its type (div, p, etc). Using a class selector (or a type selector) with that pseudo-class means to select an element if it has the given class (or is of the given type) and is the first of its type among its siblings.

Unfortunately, CSS doesn't provide a :first-of-class selector that only chooses the first occurrence of a class. As a workaround, you can use something like this:

.myclass1 { color: red; }
.myclass1 ~ .myclass1 { color: /* default, or inherited from parent div */; }

Explanations and illustrations for the workaround are given here and here.

This works if you only have 2 elements but fails with more ):

No @justNik this does work for multiple elements. The .myclass1 selector would select every element of .myclass1. The selector .myclass1 ~ .myclass1 uses the general sibling combinator to select every element with the class .myclass1 that is a following sibling of an element with a class of .myclass1. This is explained in amazing detail here.

css - CSS3 selector :first-of-type with class name? - Stack Overflow

css css3 css-selectors
Rectangle 27 557

Typically you add a class selector to the :not() pseudo-class like so:

:not(.printable) {
    /* Styles */
}

But if you need better browser support (IE8 and older don't support :not()), you're probably better off creating style rules for elements that do have the "printable" class. If even that isn't feasible despite what you say about your actual markup, you may have to work your markup around that limitation.

Keep in mind that, depending on the properties you're setting in this rule, some of them may either be inherited by descendants that are .printable, or otherwise affect them one way or another. For example, although display is not inherited, setting display: none on a :not(.printable) will prevent it and all of its descendants from displaying, since it removes the element and its subtree from layout completely. You can often get around this by using visibility: hidden instead which will allow visible descendants to show, but the hidden elements will still affect layout as they originally did. In short, just be careful.

As a little nugget of info, browser support for media-agnostic aspects of CSS is often the same across media types if a browser doesn't support :not() on the screen, it won't support it in print either.

:not()

can not

:not(div .printable)

html - Can I write a CSS selector selecting elements NOT having a cert...

html css css-selectors
Rectangle 27 540

Typically you add a class selector to the :not() pseudo-class like so:

:not(.printable) {
    /* Styles */
}

But if you need better browser support (IE8 and older don't support :not()), you're probably better off creating style rules for elements that do have the "printable" class. If even that isn't feasible despite what you say about your actual markup, you may have to work your markup around that limitation.

Keep in mind that, depending on the properties you're setting in this rule, some of them may either be inherited by descendants that are .printable, or otherwise affect them one way or another. For example, although display is not inherited, setting display: none on a :not(.printable) will prevent it and all of its descendants from displaying, since it removes the element and its subtree from layout completely. You can often get around this by using visibility: hidden instead which will allow visible descendants to show, but the hidden elements will still affect layout as they originally did. In short, just be careful.

As a little nugget of info, browser support for media-agnostic aspects of CSS is often the same across media types if a browser doesn't support :not() on the screen, it won't support it in print either.

:not()

can not

:not(div .printable)

html - Can I write a CSS selector selecting elements NOT having a cert...

html css css-selectors
Rectangle 27 546

Typically you add a class selector to the :not() pseudo-class like so:

:not(.printable) {
    /* Styles */
}

But if you need better browser support (IE8 and older don't support :not()), you're probably better off creating style rules for elements that do have the "printable" class. If even that isn't feasible despite what you say about your actual markup, you may have to work your markup around that limitation.

Keep in mind that, depending on the properties you're setting in this rule, some of them may either be inherited by descendants that are .printable, or otherwise affect them one way or another. For example, although display is not inherited, setting display: none on a :not(.printable) will prevent it and all of its descendants from displaying, since it removes the element and its subtree from layout completely. You can often get around this by using visibility: hidden instead which will allow visible descendants to show, but the hidden elements will still affect layout as they originally did. In short, just be careful.

As a little nugget of info, browser support for media-agnostic aspects of CSS is often the same across media types if a browser doesn't support :not() on the screen, it won't support it in print either.

:not()

can not

:not(div .printable)

html - Can I write a CSS selector selecting elements NOT having a cert...

html css css-selectors
Rectangle 27 5

Just thought I would throw another answer up here in case anyone else runs into this. This happened to me when I tried to call .tablesorter() on a table using a class selector instead of an id selector. So changing it from

$('.tablesorter').tablesorter();
$('#tablesorter').tablesorter();

jquery tablesorter ajax table only sorting one direction - Stack Overf...

jquery ajax sorting tablesorter
Rectangle 27 5

Just thought I would throw another answer up here in case anyone else runs into this. This happened to me when I tried to call .tablesorter() on a table using a class selector instead of an id selector. So changing it from

$('.tablesorter').tablesorter();
$('#tablesorter').tablesorter();

jquery tablesorter ajax table only sorting one direction - Stack Overf...

jquery ajax sorting tablesorter
Rectangle 27 18

Just in case somebody else have the same problem I had with NSTimer where none of the other answers fixed the issue, is really important to mention that, if you are using a class that do not inherits from NSObject either directly or deep in the hierarchy(e.g. manually created swift files), none of the other answers will work even when is specified as follows:

let timer = NSTimer(timeInterval: 1, target: self, selector: "test", 
                    userInfo: nil, repeats: false)
func test () {}

Without changing anything else other than just making the class inherit from NSObject I stopped getting the "Unrecognized selector" Error and got my logic working as expected.

The issue with this alternative is that you cant change a class (lets say ViewController) to inherit from NSObject, given that you need the ViewController class implemented stuff (e.g. viewDidLoad()). Any idea how to call a Swift function within a ViewController using NSTimer?... e

UIViewController already inherits from NSObject, most classes exposed by the SDK do, this example is for your own created classes that require NSTimer functionality...

@selector() in Swift? - Stack Overflow

swift selector nstimer
Rectangle 27 18

Just in case somebody else have the same problem I had with NSTimer where none of the other answers fixed the issue, is really important to mention that, if you are using a class that do not inherits from NSObject either directly or deep in the hierarchy(e.g. manually created swift files), none of the other answers will work even when is specified as follows:

let timer = NSTimer(timeInterval: 1, target: self, selector: "test", 
                    userInfo: nil, repeats: false)
func test () {}

Without changing anything else other than just making the class inherit from NSObject I stopped getting the "Unrecognized selector" Error and got my logic working as expected.

The issue with this alternative is that you cant change a class (lets say ViewController) to inherit from NSObject, given that you need the ViewController class implemented stuff (e.g. viewDidLoad()). Any idea how to call a Swift function within a ViewController using NSTimer?... e

UIViewController already inherits from NSObject, most classes exposed by the SDK do, this example is for your own created classes that require NSTimer functionality...

@selector() in Swift? - Stack Overflow

swift selector nstimer
Rectangle 27 18

Just in case somebody else have the same problem I had with NSTimer where none of the other answers fixed the issue, is really important to mention that, if you are using a class that do not inherits from NSObject either directly or deep in the hierarchy(e.g. manually created swift files), none of the other answers will work even when is specified as follows:

let timer = NSTimer(timeInterval: 1, target: self, selector: "test", 
                    userInfo: nil, repeats: false)
func test () {}

Without changing anything else other than just making the class inherit from NSObject I stopped getting the "Unrecognized selector" Error and got my logic working as expected.

The issue with this alternative is that you cant change a class (lets say ViewController) to inherit from NSObject, given that you need the ViewController class implemented stuff (e.g. viewDidLoad()). Any idea how to call a Swift function within a ViewController using NSTimer?... e

UIViewController already inherits from NSObject, most classes exposed by the SDK do, this example is for your own created classes that require NSTimer functionality...

@selector() in Swift? - Stack Overflow

swift selector nstimer
Rectangle 27 883

What you need is called attribute selector. An example, using your html structure, is the following:

div[class^="tocolor-"], div[class*=" tocolor-"] {
    color:red 
}

In the place of div you can add any element or remove it altogether, and in the place of class you can add any attribute of the specified element.

[class^="tocolor-"] starts with "tocolor-".[class*=" tocolor-"] contains the substring "tocolor-" occurring directly after a space character.

This doesn't take into account non-whitespace characters before the tocolor- prefix. For example it'll match class="abcdtocolor-1" even though it doesn't start with the prefix. A slightly better selector to use would be div[class^='tocolor-'], div[class*=' tocolor-'], explained here.

More information on CSS attribute selectors, you can find here and here.

Cool info. Only caveat is if performance is an issue, most CSS linters will reject attribute selectors that resemble regex (e.g. using '*') b/c of slow performance. With the exception of using a preprocessor (e.g. Sass), is there any other possible way of doing this?

Is there a way to check if the class attribute contains multiple substrings? Maybe something like div[class*="foo"][class="bar"]?

That space in our "contains" example is pretty killer. From what I can see, it shouldn't be there, correct?

css selectors - wildcard * in CSS for classes - Stack Overflow

css css-selectors wildcard
Rectangle 27 864

What you need is called attribute selector. An example, using your html structure, is the following:

div[class^="tocolor-"], div[class*=" tocolor-"] {
    color:red 
}

In the place of div you can add any element or remove it altogether, and in the place of class you can add any attribute of the specified element.

[class^="tocolor-"] starts with "tocolor-".[class*=" tocolor-"] contains the substring "tocolor-" occurring directly after a space character.

More information on CSS attribute selectors, you can find here and here.

Cool info. Only caveat is if performance is an issue, most CSS linters will reject attribute selectors that resemble regex (e.g. using '*') b/c of slow performance. With the exception of using a preprocessor (e.g. Sass), is there any other possible way of doing this?

Is there a way to check if the class attribute contains multiple substrings? Maybe something like div[class*="foo"][class="bar"]?

That space in our "contains" example is pretty killer. From what I can see, it shouldn't be there, correct?

css selectors - wildcard * in CSS for classes - Stack Overflow

css css-selectors wildcard
Rectangle 27 1

Classes that are on your original <select> are copied to the <div> generated by Select2, so you shouldn't expect to get your original <select> just by using a class selector.

What is happening right now is that you are setting the .html() of the <div> instead of the <select>, which is why it is no longer being displayed properly. Instead of doing

$(".cr_coursedescription").html(html);

You should try out

$("select.cr_coursedescription").html(html);

So it only gets the <select> element.

php - Dynamic dependent dropdown with jquery-select2 - Stack Overflow

php jquery mysql ajax jquery-select2
Rectangle 27 2

To answer my own question, this is the code which ended up working and solved my problem. Key bits of information are as follows:

document.styleSheets[ i ].href
document.styleSheets[ i ].cssRules[ j ].selectorText
document.styleSheets[ i ].cssRules[ j ].style.fontFamily

This all starts with the document.styleSheets array-like object (array-like, because it is not actually a JavaScript array), and the rest is straightforward DOM. A generic function could be made. The href could be optional, depending upon if you know where the external sheet is loaded from and you know you can safely worry about only one unique selector, then you can avoid searching through the ~10 other sheets loaded by Firefox. This is the simplest use-case.

// Load font and apply style sheet class

// Am using the .href to find the sheet faster because location is known and rule is unique
var myCSShref = 'chrome://{GUID}/skin/text.css';
var mySelector = '.myTextClass';

// fonts stored as a string in a user pref, later split to array inline
// fonts could be stored as array during runtime, remove splits accordingly
// fonts get appended to a XUL menulist element
// list index 0 is for default, 1+ for fonts below
var fonts = 'monospace|Anonymous Pro|Apple Menlo|Bitstream Vera Sans Mono|Consolas|DejaVu Sans Mono|Droid Sans Mono|Everson Mono|Liberation Mono|Lucida Console|Source Code Pro';
var font_index = 2; // Anonymous Pro, the 2 is pulled from a user pref

// for loop vars
var i, styleSheet, j, cssRule, oldFontFamily, newFontFamily;

// console.logs for demonstration purposes only

// The following code only needed to be run once for me,
// but it can easily be changed to a reusable function.

if ( document.styleSheets ) {
    for ( i = 0; i < document.styleSheets.length; i++ ) {
        styleSheet = document.styleSheets[ i ];

        console.log( 'style sheet: ' + i + ' ' + styleSheet.href );

        if ( styleSheet.href === myCSShref ) {
            for ( j = 0; j < styleSheet.cssRules.length; j++ ) {
                cssRule = styleSheet.cssRules[ j ];
                console.log( 'style sheet: ' + i + ' cssRule.selectorText: ' + cssRule.selectorText );

                if ( cssRule && cssRule.selectorText === mySelector ) {
                    oldFontFamily = ( cssRule.style.fontFamily ) ? cssRule.style.fontFamily : '';
                    // font_index 0 is the menu default option, so we have 1 extra
                    newFontFamily = ( font_index === 0 ) ? 'inherit' : fonts.split( '|' )[ font_index - 1 ];
                    console.log( 'style sheet: ' + i + ' cssRule.style.fontFamily: ' + oldFontFamily + ' (before)' );
                    console.log( 'style sheet: ' + i + ' cssRule.style.fontFamily: ' + newFontFamily + ' (after)' );
                    cssRule.style.fontFamily = newFontFamily;
                }
            }
        }
    }
}

As the original poster, it seemed obvious that I was answering my own question with the actual code I used. I did include a lot of prose, in the comments of the code. Nonetheless, I have included more verbose explanation, so that it should be very clear. Please remove the down vote if this is sufficient, or else suggest another improvement and I'll try again. Thank you for your suggestions and patience.

Yeah, but your particular code is tailored for your use-case. explaining key insights goes a long way towards avoiding cargo-culting and coding by copy&paste which plagues SO.

Not familiar with the term cargo-culting. People who code by copy and paste have only themselves to answer to. I do not answer for them. My code is tailored towards my use-case because that is what answered my question. I've seen bits and pieces answered elsewhere but scarcely as thoroughly explained. My initial confusion stemmed from reliance on some horridly broken documentation at MDN which indicates this functionality is both read-only and removed from the browser. More generic code can be developed by others fueled with the key bits of info which eluded me, and so I gathered here.

After looking up the term, I am not sure if the cargo-culting comment was directed at me as an accusation or general impression by my initial code block post, but it was hand-written code. If the bulk of my other recent posts over the past few months are any indication, I tend to drive at the "why" question quite earnestly, and only after I understand why, do I ask, "how", but more at "how best" for my given situation (best in a subjective sense relative to my experience and project, etc.). Which again, all of my answer's initial code comments indicated as much.

How to dynamically modify CSS rule set (e.g. with a class selector) fr...

javascript css dom firefox-addon xul
Rectangle 27 144

In general I fully agree with Jason's use of css selector, but in some cases you may not want to change the css, e.g. when using a 3rd party css-template, and rather prefer to add/remove a class on the element.

The following sample shows a simple way of adding/removing a class on ng-mouseenter/mouseleave:

<div ng-app>
  <div 
    class="italic" 
    ng-class="{red: hover}"
    ng-init="hover = false"
    ng-mouseenter="hover = true"
    ng-mouseleave="hover = false">
      Test 1 2 3.
  </div>
</div>
.red {
  background-color: red;
}

.italic {
  font-style: italic;
  color: black;
}

Styling on hovering is a view concern. Although the solution above sets a "hover" property in the current scope, the controller does not need to be concerned about this.

Limitation: the hover scope variable has to be uniquely named, which isn't always trivial, especially on ng-repeated elements.

@AaronCampbell: Each repeated object created by ng-repeat lives in it's own scope. So as long as you init the hover variable on the element the variable scope is individual for each object. (i edited the above example to include this).

angularjs - Change class on mouseover in directive - Stack Overflow

angularjs
Rectangle 27 4

Your CSS is wrong, you are using an ID selector instead of a class selector. The hash (#) means that you call to a ID within your HTML. But if you look at your HTML you only have a class defined on the table. To select elements with a specific class, write a period (.) character, followed by the name of the class in your CSS.

html - ASP.NET Css will not work - Stack Overflow

html css asp.net
Rectangle 27 2

It certainly is. Although CSS was indeed designed to be a companion to HTML, it was also designed to be document language agnostic, with HTML-isms defined separately from the general rules (while still being compatible), and so is the Selectors standard derived from the CSS selector syntax.

The Selectors standard, on which the Selectors API is based, begins thusly:

Selectors are patterns that match against elements in a tree, and as such form one of several technologies that can be used to select nodes in an XML document. Selectors have been optimized for use with HTML and XML, and are designed to be usable in performance-critical code.

Implementations can construct DOM trees out of HTML or XML using an appropriate parser, which can then be queried using selectors. You shouldn't have any trouble matching XML elements using selectors.

Note that while HTML and XML do in fact share ID and class semantics (you can define ID and IDREF attributes in an XSD for example), you can only match them using ID and class selectors if the Selectors API implementation knows how that particular flavor of XML defines those semantics. Since the implementation you're using is a browser, chances are the only XML flavors it understands are widely-adopted Web standards like SVG and MathML, and not for instance your in-house XML flavor. You still have your basic type selectors, attribute selectors, structural pseudo-classes and combinators at your disposal though, which should be more than enough for your needs.

Most of the limitations you'll end up running into will probably lie in the selector syntax itself, although some of these limitations are being rectified in Selectors level 4 and Selectors API level 2, with the subject indicator, :matches(), extended :not(), :nth-match(), :nth-last-match(), and :scope, among others.

Y'know, even though I had a quick scan of the selectors spec, I missed that para completely. Thanks for making me look foolish :-)

xpath - Using the selector API with XML documents - Stack Overflow

xml xpath selectors-api
Rectangle 27 6

OK, first of all I'm not sure how it works when you create a div using (document.createElement('div')), so I might be wrong now, but wouldn't it be possible to use the :target pseudo class selector for this?

If you look at the code below, you can se I've used a link to target the div, but in your case it might be possible to target #new from the script instead and that way make the div fade in without user interaction, or am I thinking wrong?

<a href="#new">Click</a> 
<div id="new">
    Fade in ... 
</div>
#new {
    width: 100px;
    height: 100px;
    border: 1px solid #000000;
    opacity: 0;    
}


#new:target {
    -webkit-transition: opacity 2.0s ease-in;
       -moz-transition: opacity 2.0s ease-in;
         -o-transition: opacity 2.0s ease-in;
                                  opacity: 1;
}

... and here's a jsFiddle

Appreciate the help, tried to manually fire this through various techniques and none seem to work. Ran across this article stackoverflow.com/questions/8101854/ and it appears they gave up as well =(

OK. I'll get back to you if I think of something else that might solve it.

css3 - CSS transition fade in - Stack Overflow

css css3 css-transitions
Rectangle 27 3

This is my approach abstracting the ID's and href's, using a generic class selector

$(function() {
  // Generic selector to be used anywhere
  $(".js-scroll-to").click(function(e) {

    // Get the href dynamically
    var destination = $(this).attr('href');

    // Prevent href=# link from changing the URL hash (optional)
    e.preventDefault();

    // Animate scroll to destination
    $('html, body').animate({
      scrollTop: $(destination).offset().top
    }, 500);
  });
});
<!-- example of a fixed nav menu -->
<ul class="nav">
  <li>
    <a href="#section-1" class="nav-item js-scroll-to">Item 1</a>
  </li>
  <li>
    <a href="#section-2" class="nav-item js-scroll-to">Item 2</a>
  </li>
  <li>
    <a href="#section-3" class="nav-item js-scroll-to">Item 3</a>
  </li>
</ul>

javascript - jQuery scroll to element - Stack Overflow

javascript jquery
Rectangle 27 3

This is my approach abstracting the ID's and href's, using a generic class selector

$(function() {
  // Generic selector to be used anywhere
  $(".js-scroll-to").click(function(e) {

    // Get the href dynamically
    var destination = $(this).attr('href');

    // Prevent href=# link from changing the URL hash (optional)
    e.preventDefault();

    // Animate scroll to destination
    $('html, body').animate({
      scrollTop: $(destination).offset().top
    }, 500);
  });
});
<!-- example of a fixed nav menu -->
<ul class="nav">
  <li>
    <a href="#section-1" class="nav-item js-scroll-to">Item 1</a>
  </li>
  <li>
    <a href="#section-2" class="nav-item js-scroll-to">Item 2</a>
  </li>
  <li>
    <a href="#section-3" class="nav-item js-scroll-to">Item 3</a>
  </li>
</ul>

javascript - jQuery scroll to element - Stack Overflow

javascript jquery
Rectangle 27 3

This is my approach abstracting the ID's and href's, using a generic class selector

$(function() {
  // Generic selector to be used anywhere
  $(".js-scroll-to").click(function(e) {

    // Get the href dynamically
    var destination = $(this).attr('href');

    // Prevent href=# link from changing the URL hash (optional)
    e.preventDefault();

    // Animate scroll to destination
    $('html, body').animate({
      scrollTop: $(destination).offset().top
    }, 500);
  });
});
<!-- example of a fixed nav menu -->
<ul class="nav">
  <li>
    <a href="#section-1" class="nav-item js-scroll-to">Item 1</a>
  </li>
  <li>
    <a href="#section-2" class="nav-item js-scroll-to">Item 2</a>
  </li>
  <li>
    <a href="#section-3" class="nav-item js-scroll-to">Item 3</a>
  </li>
</ul>

javascript - jQuery scroll to element - Stack Overflow

javascript jquery