1
$("[class^='stage']").addClass("hidden");

The above hides:

<div class="stage1">
<div class="stage2">
<div class="stage1 beep">
<div class="stage2 beep">

But it misses:

<div class="beep stage1">
<div class="beep stage2">

So I've used:

$("[class*='stage']").addClass("hidden");

The problem is that I now hide even:

<div class="beep vastage">
<div class="destagel beep">

How can I design my selector so it brings me stuff that "contains stuff that starts with stuff"?

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438

2 Answers2

1

Why don't you do:

$('*').filter(function () { 
  return $(this).attr('class') !== undefined ?
             $(this).attr('class').match(/\bstage\d*\b/g) : false;
}).hide();
mpcabd
  • 1,813
  • 15
  • 20
  • Woah... This was cumbersome. It solves the problem, of course, but I think it'll be better to change the class name into something that differs from the other names entirely. The syntax will improve the readability. Nevertheless - good answer! +1 – Konrad Viltersten Jun 07 '14 at 14:02
  • 1
    Surely this answer is the worst way to handle such an issue, because it will iterate through all the elements of the document, it's not fast at all, a better solution would be to change something in the HTML to make the solution much quicker, just like @KonradViltersten said. – mpcabd Jun 07 '14 at 14:25
0

According to Jquery Documentation (http://api.jquery.com/attribute-contains-word-selector/), you can use the following syntax :

jQuery( "[attribute~='value']" )

to select a word (delimited by space) containing a string... but not "starting with", just containing.

I think the best way is to use javascript indexof to locate the string you want, and check if the index is 0 or the preceding character is white space. You need a loop on all the html code to check all elements...