JQuery filter () - filtering by multiple attributes

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

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

Should this code not do the trick ... it doesn't work.

$(".someClass").filter("#"+id, "[level='"+level+"']").someAction();
+3
source share
4 answers

You provide two arguments:

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

You probably want:

.filter("#someId[level=someLevel]")
+2
source

What about

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

?

+3
source

, , ​​ :

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;
};

:

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

Source: https://habr.com/ru/post/1740283/


All Articles