Mootools: how to select an element with two classes string

Hi X-perts,

I need to select an element with class="trigger item4" via Mootools selector. For a single class it looks like


what is a correct syntax for two classes?

Thank you
Who is Participating?
sh0eConnect With a Mentor Commented:
The basic selector syntax is basically the same.
If you want more details you should read MooTools Docs and MooTorial, for fancier traversal.

//But why? This is what .each is made for.

Open in new window

Pravin AsarPrincipal Systems EngineerCommented:
<title>Document/Page Title</title>
<style type="text/css">
div.first { background-color: #4499dd; }
div.second { background-color: #9944dd; }
div.third { background-color: #dd4499; }
<script language="javascript" src="mootools-1.2-core-nc.js">
<script language="javascript">
var myFirstElement  = new Element('div', {id: 'myFirstElement', 'class': 'first'});
var mySecondElement = new Element('div', {id: 'mySecondElement', 'class': 'second'});
var myThirdElement  = new Element('div', {id: 'myThirdElement', 'class': 'third'});
myFirstElement.innerHTML = 'First div';
mySecondElement.innerHTML = 'Second div';
myThirdElement.innerHTML = 'Third div';
var body = $(document.body);
body.adopt (myFirstElement);
body.adopt (mySecondElement);
body.adopt (myThirdElement);
var dElements = $$('.first, .second');
var ix=0;
for (ix=0; ix < dElements.length; ix++) {
      alert ( dElements[ix].className);
andy7789Author Commented:
sorry, we are talking about different things. My question is:

How to select an element by its class, when the class contains two classes, like class="first second"?

In other words:

<div class="first second">something</div>
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

ziffgoneConnect With a Mentor Commented:
Try this:
<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "">
<html xmlns="">
<style type="text/css">
body {font-family:arial;}
.first {font-weight:bold}
.second {color:red;}
<script type="text/javascript" src="mootools.js"></script>
<script type="text/javascript">
window.addEvent('domready', function() {
    var elems=$$('p');
    for(var i=0;i<elems.length;i++){
       if(elems[i].hasClass('first second')){
<p id="test_1" class="first">... Type content here!</p>
<p id="test_2" class="first second">... Type content here!</p>
<p id="test_3" class="second">... Type content here!</p>

Open in new window

andy7789Author Commented:
I do not like the idea of making a cycle here.

can we call it somehow directly? For example, in jquery you can easily select an element like this:

$("tr[class='child1 child2 child3']"). etc

It selects all tr elements with class "child1 child2 child3"

How can I do the same in mootools ?


Open in new window

Oh, quite nice Sh0e.

Two notes however,

1- You can leave out the 'p' selector and simply find the '.first.second' element.

2- This still returns an array of a single element, see code below:

var obj=$$('.first.second');
var _obj1=obj[0];
// This will return the requested element

Open in new window

andy7789Author Commented:
Thank you guys. i have not tested it yet, but, if it works, it would be a great contribution to the mootools syntax. I have been searching docs and net for a long time, but could not find a simple answer for this.

it was one of the reasons, why often I used jquery + mootools as cold not find a simple solution via mootools.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.