0

I'm currently building a filter with 5 options. The filter has an img with a 'prev' and 'next' div on either side. When you click either div, the img src changes to the adjacent src stored in an array. This works fine. Now I have a second filter. This filter will also have 5 options, except the 5 options available will depend on the option selected on the first filter. So filter1>option1 will result in options 1,2,3,4 and 5 being available on the second filter. filter1>option2 will result in options 6,7,8,9 and 10 being available on the second filter.

I'm trying to use the .onload listener so that when the filter one img loads, if filter1 source == A then filter two img src is pulled from array 1. If filter1 source == B then filter two img src is pulled from array 2.

I thought I'd explain my problem in pseudo for clarity. Here is a jsfiddle of my progress so far. http://jsfiddle.net/XujYW/

Thanks in advance!

3
  • 1
    You have a lot of code there. Isolate the problem a little bit to be more fair and easy for us to help you. And the example seems to be broken. Make an example that shows clearly what's your problem. Commented Feb 1, 2014 at 15:43
  • Ok thanks thinklinux. I will simplify the code now, although the reason it appears broken is because I am using images which are stored on my hard drive and I am unsure how to load these into jsfiddle. Commented Feb 1, 2014 at 15:45
  • I have simplified the code as much as I can, however I think it's important to see how the first filter works in order to understand the second filter, so I can't remove loads. I've replaced what code I can with notes, however this means that the fiddle won't work anyway. jsfiddle.net/XujYW/1 Commented Feb 1, 2014 at 15:53

1 Answer 1

1

The most important thing that is wrong in your code, is that you missinterpreted what $(function() { ..}) does. This is just a shortcut for $(document).ready(). It's fired after loading the page and NOT when anything changes on the page.

Then you use a very complicated way to find out the state of your list. You should have a look at jquerys data functions which allow you to bind data directly to an element. That's way more useable than your reading of img src and parsing it to get a simple index number. Look here (I left this approach as it is).

Next, you are repeating your code over and over. This makes it hard to handle especialy if you want to change anything. Better split reapeating parts of code into functions. This is what @thinklinux was requesting.

Anyhow, here is a working plunker (I prefer plunker because it's easier to debug and has a built in beautifier which makes code easier to read for those who want to help you).

I set up your values to arrays like that:

  // A global array for convenience 
  subs = new Array();
  subs['Shelter'] = ['shelter-one', 'shelter-two', 'shelter-three', 'shelter-four', 'shelter-five'];
  subs['Food'] = ['food-one', 'food-two', 'food-three', 'food-four', 'food-five'];
  subs['Energy'] = ['energy-one', 'energy-two', 'energy-three', 'energy-four', 'energy-five'];
  subs['Transport'] = ['transport-one', 'transport-two', 'transport-three', 'transport-four', 'transport-five'];
  subs['Economy'] = ['economy-one', 'economy-two', 'economy-three', 'economy-four', 'economy-five'];
  setup_effects();

  var filterOne = ['shelter', 'food', 'energy', 'transport', 'economy'];

An handle everyting over a single function:

  //Now setup the master filter
  //Parameters are:
  // Element to change
  // Array to use
  // Master filter
  // update on apply
  applyfilter('filter-one', filterOne, true, false);

This function itself calls apllyfilter with the last two parameters changed. This is not a very elegant approach (think spaghetticode!), but at least it works. Find the full code here

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

5 Comments

Thanks mainguy. I obviously misunderstood thinklinux. I was trying to work out a way to simplify that code but I'm relatively new to js and know even less about jquery. These filters will be for a map, I'm using a jquery plugin which loads the google maps api into a div, then it pulls data from a google fusion table database. I've got the database set up and tested it without filters and its working, and I'm guessing that the way I would go about loading them would be wrong. Do you think you could point me in the right direction please? I'm not sure if its obvious without knowing the plugin.
Sorry, you can keep the tick and the +1 because you did solve the problem, but now each time the first filter is changed, the second filter image won't display. I can use the arrows to loop round and it displays fine, it just doesn't seem to be defining an image src for the second filter any time that the first filter is changed. It just looks like it's set to "#"
oops, bug on my side. I forgot to add the global images source in the second line of applyfilter. Should be: $('#' + element + '-img').attr('src', imgSrcBase+values[0] + '.png');
Updated plunker is here: plnkr.co/edit/7D5cC9EVwWv8ebYLYG8Z?p=preview I need a moment to understand yor map question. Maybe add a new question? I will look after your case ...
Thanks mainguy :) This project has been put on hold for the time being whilst we work on it's parent website. I'm sure that I'll utilise this at some point :)

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.