How to select all elements with an attribute starting with

advertisements

I am trying to select all the elements that start with the "box". This is my existing code:

if (($(this).attr('title') == 'Box a') || ($(this).attr('title') == 'Box b')) || ($(this).attr('title') == 'Box c')) {
// do stuff
 }

Is there a way to shorten this?

Thanks


Using CSS symbols it is possible!

The ^ symbol.

$(element[title^='box']);

That selects all element elements that the title field starts with box.

UPDATE:

Try this.

var regEx = /^Box/;

if(regEx.test($(this).attr('title'))){

}