0

I have the following list of elements.

 <tr id="level_0">
 <tr class="level_0_1">
 <tr class="level_0_2">
 <tr class="level_0_2_1">
 <tr class="level_0_2_2">
 <tr class="level_0_2_3">
 <tr class="level_0_2_3_1">
 <tr class="level_0_2_3_2">

I am trying select all the elements that have class level_0_2_X and not level_0_2_X_X

Using the jQuery "starts with" selector I am able to select all the elements that have class level_0_2_X and level_0_2_X_X

   $("tr[class^='level_0_2_']").show();
1
  • 1
    Please be careful when posting questions. You kept referring to your class as id. I edited it accordingly to match your code. Commented Feb 14, 2013 at 19:07

2 Answers 2

6

You'd better combine a selector with a filtering function :

$('[id^=level_0_2_]').filter(function(){ return this.id.split('_').length<5 })

If what you want is in fact a filtering on the class, you may use this :

$('[class^=level_0_2_]').filter(function(){
    return this.className.split('_').length<5
})
Sign up to request clarification or add additional context in comments.

4 Comments

doh, you beat me to it . +1
Another bikeshed here...:)
The question isn't clear as it mix id and class. Maybe an adjustment would be needed.
@dystroy It throws TypeError: this.classname is undefined. Thanks for your help.
1

Try using match(). Following must get the job done for you:

$('tr').each(function(){
     if( $(this).attr('id').match(/pattern/) ) {
          $(this).show();
     }
  }

Just use the pattern as required. Also above I have used 'id', you get the 'class' attribute if required and match against that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.