Treehouse

Create a Custom “:selected” Pseudo Selector in MooTools

By on  

A while back I read a very interesting article by MooTools core developer Jan Kassens about how to create a custom pseudo selector in MooTools. I was surprised at the ease in which one can add their own pseudo selector that I thought I'd add a pseudo selector that I found useful in jQuery: :selected. This pseudo selector would return option elements that are selected. Here's how I did it:

The XHTML

<select name="mysel" id="mysel" multiple="multiple">
	<option value="One">One</a>
	<option value="Two">Two</a>
	<option value="Three" selected="selected">Three</a>
	<option value="Four" selected="selected">Four</a>
</select>

A basic select element that allows for multiple selections and has two elements selected by default.

The MooTools Custom Pseudo Selector

	Selectors.Pseudo.selected = function(){
		return (this.selected && 'option' == this.get('tag'));
	};

MooTools has an internal "Selectors.Pseudo" variable so all I needed to do was add a selected property. That property is a function that returns whether an individual returned elements should be added to the collection. In this case, if the element has the selected property as true, the element should be returned. Using my example above, the 2 matching elements will be returned.

What other pseudo selectors do you think would be useful in MooTools? Please share!

ydkjs-6.png

Recent Features

  • Animated 3D Flipping Menu with&nbsp;CSS

    CSS animations aren't just for basic fades or sliding elements anymore -- CSS animations are capable of much more.  I've showed you how you can create an exploding logo (applied with JavaScript, but all animation is CSS), an...

  • From Webcam to Animated GIF: the Secret Behind&nbsp;chat.meatspac.es!

    My team mate Edna Piranha is not only an awesome hacker; she's also a fantastic philosopher! Communication and online interactions is a subject that has kept her mind busy for a long time, and it has also resulted in...

  • Create a CSS&nbsp;Cube

    CSS cubes really showcase what CSS has become over the years, evolving from simple color and dimension directives to a language capable of creating deep, creative visuals.  Add animation and you've got something really neat.  Unfortunately each CSS cube...

Incredible Demos

  • Create Spinning Rays with CSS3 Animations &#038;&nbsp;JavaScript

    Thomas Fuchs, creator of script2 (scriptaculous' second iteration) and Zepto.js (mobile JavaScript framework), creates outstanding animated elements with JavaScript.  He's a legend in his own right, and for good reason:  his work has helped to inspire developers...

  • Face Detection with&nbsp;jQuery

    I've always been intrigued by recognition software because I cannot imagine the logic that goes into all of the algorithms. Whether it's voice, face, or other types of detection, people look and sound so different, pictures are shot...

  • CSS content and&nbsp;attr

    CSS is becoming more and more powerful but in the sense that it allows us to do the little things easily.  There have been larger features added like transitions, animations, and transforms, but one feature that goes under the radar...

Discussion

  1. Evan

    Interesting. I can defiantly see using this one.

    What would happen if I tried to use that pseudo selector on an element that is not a select? Do you think it would be a good idea to wrap it in an if statement? Also could we return the selected element with the MooTools element extensions already applied?

    if(this.get(‘tag’) == ‘select’){
    return $(this.selected);
    }

  2. @Evan: Per your comment:

    1. Good question, didn’t try that.
    2. Good idea.
    3. I don’t believe you need to do that.
  3. @Even: Actually, let me reconsider my response:

    There should be a check to make sure it’s a “select” element. I’ll add that and test when I get a free moment. Your “return $()” is wrong though. All that needs to be returned within the pseudos is a “true” or “false” value. Essentially, is the condition a match or no?

  4. @Evan: One more update. You want to make sure the tag is an “option” tag, not a “select” tag.

  5. Grammar Nazi

    Interesting. I can defiantly see using this one.

    The word is definitely… Why do so many people have trouble spelling this word?

  6. Evan

    @Grammar Nazi… I noticed the stupid spelling mistake right after I posted and could not go back to edit…

    @david Thanks for testing out my questions for me.

  7. really nice one, it look good

  8. MooTools has functionality built in to get selected elements:

    http://mootools.net/docs/Element/Element#Element:getSelected

  9. @atom: Yep, I guess I find “:selected” to be shorter and within the $$() functionality.

  10. This is one of the neatest tricks I’ve seen in a while. I can already think of two situations where I’d use this right away! Keep up the good work Mister Walsh.

    Oh, and when I read that someone would “defiantly” use this, I just figured they were being rebellious, audacious, insubordinate … mutinous. Using this technique with recalcitrant disobedience.

    “Definitely” is spelled right, but much less interesting.

  11. Ha, nice script, in my current style of programming I don’t use it, but defiantly will later on!

    Sorry Grammar, couldn’t resist :)

  12. If MooTools implement CSS3 for it’s selector the :checked could be used.

    While the :checked pseudo-class is dynamic in nature, and is altered by user action, since it can also be based on the presence of the semantic HTML4 selected and checked attributes, it applies to all media.

    See http://www.w3.org/TR/css3-selectors/#UIstates

  13. Yann

    Do you know your script doesn’t work on IE ?

  14. Luke Hoggett

    In response to Yann’s comment to have this pseudo selector and other play nice with IEs you might like to have a look at this Mootools Core ticket on Lighthouse

    from Valerio’s comment there the script would need to be something like (note haven’t tested)

    Selectors.Pseudo.selected = function(){
        return (this.selected && 'option' == Element.get(this, 'tag'));
    };
    
  15. Thunder Mountain

    For IE and FF support you can wrap it in a Try/Catch

    Selectors.Pseudo.selected = function(){
        var tag;
    
        try {
            tag = Element.get(this, 'tag');
        } 
        catch (ex) {
            tag = this.get('tag');
        }
    
         return (this.selected && ‘option’ == tag);
    );
    
  16. [...] Skapa en anpassad ": vald" Pseudo Selector i MooTools [...]

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!

Use Code Editor