0

I want to search for nested divs like these in my DOM

 <div class="two-columns some-other-class">
      <div class="two-columns some-other-class">
      </div>
 </div>

I tried to search nested divs like this

$("div[class$='columns'] div[class$='columns']")

but it's not working :(

  • I'd reccomend not using regex for this. See: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – Martyn Apr 10 '14 at 10:43
  • @Sandeep: Never change the question when people have answered it, instead make a EDIT – Ajinkya Apr 10 '14 at 10:54
  • 1
    Thanks for pointing it out @Karna .... I'm new to this space and didn't know this part. Will take care next time. – Sandeep Choudhary Apr 11 '14 at 08:42

2 Answers2

3

Use the Attribute Contains Selector:

$("div[class*='columns'] div[class*='columns']")

Edit:

If you want that exact functionality you might want to extend the jQuery selector engine:

$.extend($.expr[':'], {
    classEndsWith: function(e, i, meta) {
        var found = false
        var classes = e.className.toLowerCase().split(' ');
        var suffix = meta[3].toLowerCase();
        $.each(classes, function(i, c) {
            // Check if className ends with value in suffix
            if (c.indexOf(suffix, c.length - suffix.length) !== -1) {
                found = true;
                return false;
            }
        });
        return found;
    }
});

var element = $('div:classEndsWith(columns) div:classEndsWith(columns)');

See JSFiddle.

Sani Huttunen
  • 23,620
  • 6
  • 72
  • 79
0
$("div[class$='columns'] div[class$='columns']")

Is working. Check the fiddle

Ajinkya
  • 22,324
  • 33
  • 110
  • 161