Peter
Peter

Reputation: 745

Making jQuery completely case-insensitive

I'm using jQuery for automated testing. The HTML for different parts of the application looks very much alike, but sometimes capitalization of class names, ids, and styles is different.

For instance, the class for result tables is sometines 'Grid', sometimes 'grid'.

So I need to use different jQuery expressions in the test code (in Java):

public String getResultCell(int row, int colum) throws Exception {
  return _browser.evaluateJavaScript("$('table.Grid').find('tbody').find('tr').eq(" + row + ").find('td').eq(" + colum + ").text()").trim();
}

And sometimes

// FIXME: fix case sensitivity
public String getResultCell(int row, int colum) throws Exception {
  return _browser.evaluateJavaScript("$('table.grid').find('tbody').find('tr').eq(" + row + ").find('td').eq(" + colum + ").text()").trim();
}

Is there a way to make jQuery case-insensitive altogether?

Upvotes: 0

Views: 82

Answers (1)

paulitto
paulitto

Reputation: 4673

This jquery code should find table by class name and not depend on case

var classname = 'grid';
$('table').filter(function(){
   return (" "+$(this).get(0).className.toLowerCase()+" ".indexOf(' '+classname.toLowerCase()+' ')!=-1);
});

You may wrap this as a jquery method:

$.fn.filterByClass = function (classname) {
    return $(this).filter(function(){
       return (" "+$(this).get(0).className.toLowerCase()+" ".indexOf(' '+classname.toLowerCase()+' ')!=-1);
    });
};

and then use this like this: $('table').filterByClass('grid')

see demo

Upvotes: 1

Related Questions