3

I want to filter based on two attributes, the first is id and the second is called level

i.e. id="someId" level="someLevel"

Shouldn't this code do the trick ... it doesn't seem to work.

$(".someClass").filter("#"+id, "[level='"+level+"']").someAction();
1
  • Btw: I want to do the equivalent of an SQL AND statement not an OR statement. Commented Apr 8, 2010 at 8:57

4 Answers 4

3

You are providing two arguments:

.filter("#someId", "[level='someLevel']")

You probably want this:

.filter("#someId[level=someLevel]")
Sign up to request clarification or add additional context in comments.

Comments

3

what about

$(".someClass").filter("filter1").filter("filter2") 

?

Comments

2

You can use a Multiple Attribute Selector in your filter function.

Comments

0

If its an or condition you should really have a helper function like string format:

string.Format = function( text )
{
    //check if there are two arguments in the arguments list
    if ( arguments.length <= 1 )
    {
        //if there are not 2 or more arguments there’s nothing to replace
        //just return the original text
        return text;
    }
    //decrement to move to the second argument in the array
    var tokenCount = arguments.length - 2;
    for( var token = 0; token <= tokenCount; token++ )
    {
        //iterate through the tokens and replace their placeholders from the original text in order
        text = text.replace( new RegExp( "\\{" + token + "\\}", "gi" ),
                                                arguments[ token + 1 ] );
    }
    return text;
};

and then its easy:

var filter = string.format("#{0}, [level='{1}']", id, level);
$(".someClass").filter(filter).someAction();

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.