sardine sardine - 1 year ago 47
Javascript Question

If condition that pushes values that contain/match a string

I have a nicely functioning full calendar script. I have some filters for it, which basically have the following form:

$("input[name='event_filter_select']:checked").each(function () {
// I specified data-type attribute in HTML checkboxes to differentiate
// between risks and tags.
// Saving each type separately
if ($(this).data('type') == 'risk') {
} else if ($(this).data('type') == 'tag') {

However the
else if
statement should check if the checked value 'tag' is contained within the result set, not be the only value of the result set (as implied by the

Now I can only filter results that have the checked tag-value only. But i want to filter those, which have the tag-value amongst many others.

I figure this is to be done with
but i cannot figure out for the life of me how to put that into an if-statement.

Would be really glad if someone could lead me in the right direction.

Answer Source

I would simply do:

if ($(this).data('type') == 'risk') {
} else if ($(this).data('type').test(/^tag/) {

This works if the 'tag' must be at the beginning of the string.
If the 'tag' can be everywhere in the string, you can use test(/tag/).