0

I have two select lists and need to change the value of one based on their current state so:

Select list 1:

Option A (All)
Option B (12)
Option C (13)

Select list 2:

Option 1 (All)
Option 2 (14)
Option 3 (15)
Option 4 (16)

If select list 1 = Option C && select list 2 = Option 2 then change select list 1 to Option A.

I have the following code:

$('#edit-lines, #edit-trading-options').change(function () {
  if ($('#edit-lines').val().indexOf(13) && $('#edit-trading-options').val().indexOf(14) > -1) $('#edit-trading-options').val('All');
});

But something's not right. The second select stays at 'All' all the time.

Thanks,

Steve

4
  • 2
    instead of .val().indexOf(13), you should use .val() == 13. Commented Nov 15, 2013 at 18:30
  • can you make a fiddle of your situation.? Commented Nov 15, 2013 at 18:32
  • 1
    Or if you must use .indexOf, you need to use != -1, like you did for the second test. Commented Nov 15, 2013 at 18:32
  • Is this a multi-select? Commented Nov 15, 2013 at 18:33

2 Answers 2

2

If just those 2 specific options, then do this:

$('#edit-lines, #edit-trading-options').change(function () {
  if ($('#edit-lines').val() == 13 && $('#edit-trading-options').val() == 14)
     $('#edit-trading-options').val('All');
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks David. Just tried in fiddle jsfiddle.net/mj9L7/1 and works great. I also have an AJAX layer that auto submits when there is a state change to either select list. Using the code in dev, it doesn't work until I refresh the page. I'm assuming this is related? Any ideas how I could prevent this?
So glad that worked for you. Not sure I understand your follow-up question. Could you provide more detail?
0

I assume that #edit-lines is the first select, so why do you change the value of the second one?
Also, the first condition in the if statement is wrong. It would always return TRUE, since you're comparing strings with numbers (number 13 passed as the indexOf's argument).
You don't need to use .indexOf() method. Simply compare the values.

Here's the example:

$('#edit-lines, #edit-trading-options').change(function () {
    if ($('#edit-lines').val() == 13 && $('#edit-trading-options').val() == 14)
        $('#edit-lines').val('All');
});

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.