Setting up a bunch of listeners with a looping structure
Hi everyone,
I'm trying to come up with a better architecture for dealing with multiple listeners in a multi-page questionnaire. Here's the general structure I'm looking to implement:
1. Set up CSS for animation
2. Look for preconditions
3. Initialize listener
4. Execute animation
Some issues to consider:
1. Some questions have dependencies that show or hide depending on what is selected in a parent question.
2. Needs to be IE 6 compatible.
3. It should be simple enough so that all you need to do is add a new question to a JSON object, which is what I have set up right now.
Some code (the looping structure that reads each question):
- var newQ = eval(q);
$.each(newQ, function () {
// 1 - Setting CSS for each listener
$(this.css).css({'width': '1500px', 'float': 'left', 'clear':'both'});
// 2 - Applying pre-conditions
if (this.value.length > 1) {
// handle multiple checkbox conditionals
}
else {
if ($('[name="'+this.name+'"]:checked').attr('value') != this.value) $('#'+this.eid).hide();
}
});
An example of a question with dependencies and multiple conditionals. Each value represents a checkbox that needs to be checked to see if it's selected or not.
- {
'key': '2030',
'name': 'q2030',
'value': [
'value1',
'value2',
'value3',
...
- 'value8'
],
'css': '#_layoutCSS',
'eid': 'hideme',
'toggle': 'false'
},
The problem here is that when I do a console.dir on $.each(this.value.length), it counts each character, as opposed to counting the number of elements in the array. Should I be doing an eval on q? Why or why not? If I could get this up and running, it would make my life 100x easier, as there are hundreds of questions, and I'd want to make this all data driven.
Thanks!
Eugene "Eggers"