1

I'm creating a filter using select elements. This is for a wheel store, so the values would be bolt patterns, sizes, colors, etc.

I have to make an AJAX call with all the selected values in the format:

value1+value2+value3....

The only way I could think of to do this would be to iterate over the selected options and add to the sign + and the selected value to the string and in the end use substring to remove the first + sign.

var SelectedFilters = '';
$('#CategoryFilter .BlockContent select').each(function(index, element) {
    value = $(element).find('option:selected').val();
    if(value != "Choose One"){
        SelectedFilters += ('+' + value);  // This is the line with the problem
    });
 SelectedFilters = SelectedFilters.substring(1,SelectedFilters.length);

The problem I'm having is with line 5 above. I'm getting a Syntax, unexpected token error, but I cannot figure out what is wrong with my syntax.

2 Answers 2

3

There's nothing wrong with that line, but there's something wrong with the next line:

    });

If that's supposed to be the end of the .each() callback, then you're missing the } for the if statement. If it's not supposed to be the end of the function, then the ); is wrong.

Sign up to request clarification or add additional context in comments.

1 Comment

Ahh, that was it. Thank you Pointy. I will accept as answer as soon as it lets me
1

You have some syntax errors, use JSLint or JSHint to fix them.

Also, you can greatly simplify this process:

var SelectedFilters = $('#CategoryFilter .BlockContent option:selected')
    .filter(function ()  { return this.value !== 'Choose One'; })
    .map(function () { return this.value; }).get().join('+');

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.