0

This pertains to my question yesterday, which was answered perfectly here by @iambriansreed.

In trying to take it one step further, my brain short-circuited. Plus, the jquery api site is down.

I have two select boxes; certain options in the second are removed based on the selection of the first. Option values of each select box are arrays.

What I need to do now is CHANGE the value of the second selectbox so that any array segment that doesn't match the first is removed. I've been on it all day and it's twisted my head. So, in this fiddle, if "Chamber Music" is selected in #selectbox1, the value of #selectbox2's option Chamber Music 5 should change to

<option value="620069,1547340">Chamber Music 5</option>, 

since the other value (328874) wasn't a match in the original array. Possible? Clunky? I'm already comparing arrays, so already getting non-matches.

dilettante
  • 381
  • 1
  • 4
  • 19
  • Make sure your html is correct. I don't know exactly what you're trying to do, but your html labels `Chamber Music (6)` while it only has 5 options, being that the `Chamber Music 5` has 3 values in it - including `328874` which is repeated on `Orchestral 1`. – Fabrício Matté May 06 '12 at 00:18
  • Thanks for this. To clarify, you should ignore those labels; the correct data will be pulled from the database. And there will be repeats-- multiple values for some of these options. So everything posted to that fiddle is working so far-- I just have no idea how to remove individual unmatched segments from the arrays, then rewrite the options to reflect the new arrays. Thanks though! – dilettante May 06 '12 at 00:47

2 Answers2

1

I'm not quite sure I understand the question, specifically the "the value of #selectbox2's option Chamber Music 5 should change to Chamber Music 5" part. What I assume is that you want to change the value of the option in the second select box, depending on the selected values in the first select box.

Something like this would work: http://jsfiddle.net/3VfU4/3/

// check each value
var values = this.value.split(',');
var new_values = [];
var result = true;

for(i in values) {
    if($.inArray(values[i], selected) > -1) {
        result = false;
        new_values.push(values[i]);
    }
}

this.value = new_values.join(',');

return result;
Christian
  • 19,605
  • 3
  • 54
  • 70
  • This does it, perfectly. Sorry, I had a bad paste job above. Going to edit the question so that others can make sense of it. Thanks for the good job interpreting (I'm 9 months pregnant; my brain gives out), and the even better job fixing the code. – dilettante May 06 '12 at 01:44
  • Well by the sounds of things javascript will soon be the least of your worries :). Congratulations! – Christian May 06 '12 at 14:29
0

I'm afraid I'm not familiar with jQuery enough to help you, but I thought I could share what I do when an api site is down.

You can still view the page by looking at the google cached page. If you are using Chrome, it's an option on the Page Not Found screen.

Or for other ways, look here: http://www.blueglass.com/blog/everything-you-should-know-about-google-cache/

dallin
  • 8,775
  • 2
  • 36
  • 41