Ryan Coleman Ryan Coleman - 4 months ago 9
HTML Question

Why is my jQuery function only opening one link and not the others listed in the code?

I'm currently implementing a script into a website I'm working on that checks to see which checkboxes the user has checked and then once the user clicks submit it downloads said files. The issue I'm having is, no matter how many of the checkboxes are clicked, only the first file downloads and none of the others.

Here's my code:

// Global manual variables
var classic;
var racing;
var flexer;
// Global handbook variables
var classicEng;
var classicMulti;
var flexerEng;
var racerEng;

// Checkbox download function

jQuery(document).ready(function() {
classic = jQuery('#edit-submitted-manuals-required-1');
racing = jQuery('#edit-submitted-manuals-required-2');
flexer = jQuery('#edit-submitted-manuals-required-3');
classicEng = jQuery('#edit-submitted-handbooks-required-1');
classicMulti = jQuery('#edit-submitted-handbooks-required-2');
flexerEng = jQuery('#edit-submitted-handbooks-required-3');
racerEng = jQuery('#edit-submitted-handbooks-required-4');

jQuery('#edit-submit').click(function() {
if (classicEng.is(':checked') && classicMulti.is(':checked') && flexerEng.is(':checked') && racerEng.is(':checked')) {
window.open('/sites/default/files/manuals/classic-xl-45-english.pdf', '_blank');
window.open('/sites/default/files/manuals/classic-xl-45.pdf', '_blank');
window.open('/sites/default/files/manuals/flexer-45-english.pdf', '_blank');
window.open('/sites/default/files/manuals/racing-45-english.pdf', '_blank');

} else if (classicEng.is(':checked') && classicMulti.is(':checked') && flexerEng.is(':checked')) {
window.open('/sites/default/files/manuals/classic-xl-45-english.pdf', '_blank');
window.open('/sites/default/files/manuals/classic-xl-45.pdf', '_blank');
window.open('/sites/default/files/manuals/flexer-45-english.pdf', '_blank');

} else if (classicEng.is(':checked') && classicMulti.is(':checked')) {
window.open('/sites/default/files/manuals/classic-xl-45-english.pdf', '_blank');
window.open('/sites/default/files/manuals/classic-xl-45.pdf', '_blank');

} else if (classicEng.is(':checked') && flexerEng.is(':checked')) {
window.open('/sites/default/files/manuals/classic-xl-45-english.pdf', '_blank');
window.open('/sites/default/files/manuals/flexer-45-english.pdf', '_blank');

} else if (classicEng.is(':checked') && racerEng.is(':checked')) {
window.open('/sites/default/files/manuals/classic-xl-45-english.pdf', '_blank');
window.open('/sites/default/files/manuals/racing-45-english.pdf', '_blank');

} else if (classicMulti.is(':checked') && flexerEng.is(':checked') && racerEng.is(':checked')) {
window.open('/sites/default/files/manuals/classic-xl-45.pdf', '_blank');
window.open('/sites/default/files/manuals/flexer-45-english.pdf', '_blank');
window.open('/sites/default/files/manuals/racing-45-english.pdf', '_blank');

} else if (classicMulti.is(':checked') && flexerEng.is(':checked')) {
window.open('/sites/default/files/manuals/classic-xl-45.pdf', '_blank');
window.open('/sites/default/files/manuals/flexer-45-english.pdf', '_blank');

} else if (classicEng.is(':checked') && racerEng.is(':checked')) {
window.open('/sites/default/files/manuals/classic-xl-45.pdf', '_blank');
window.open('/sites/default/files/manuals/racing-45-english.pdf', '_blank');

} else if (flexerEng.is(':checked') && racerEng.is(':checked')) {
window.open('/sites/default/files/manuals/flexer-45-english.pdf', '_blank');
window.open('/sites/default/files/manuals/racing-45-english.pdf', '_blank');

} else if (classicEng.is(':checked')) {
window.open('/sites/default/files/manuals/classic-xl-45-english.pdf', '_blank');

} else if (classicMulti.is(":checked")) {
window.open('/sites/default/files/manuals/classic-xl-45.pdf', '_blank');

} else if (flexerEng.is(':checked')) {
window.open('/sites/default/files/manuals/flexer-45-english.pdf', '_blank');

} else if (racerEng.is(':checked')) {
window.open('/sites/default/files/manuals/racing-45-english.pdf', '_blank');

} else if (classic.is(':checked') && racing.is(':checked') && flexer.is(':checked')) {
window.open('/sites/default/files/manuals/classic-xl-parts.pdf', '_blank');
window.open('/sites/default/files/manuals/racing-45-parts.pdf', '_blank');
window.open('/sites/default/files/manuals/flexer-xl-parts.pdf', '_blank');

} else if (classic.is(':checked') && flexer.is(":checked")) {
window.open('/sites/default/files/manuals/classic-xl-parts.pdf', '_blank');
window.open('/sites/default/files/manuals/flexer-xl-parts.pdf', '_blank');

} else if (classic.is(':checked') && racing.is(":checked")) {
window.open('/sites/default/files/manuals/classic-xl-parts.pdf', '_blank')
window.open('/sites/default/files/manuals/racing-45-parts.pdf', '_blank');

} else if (racing.is(':checked') && flexer.is(':checked')) {
window.open('/sites/default/files/manuals/racing-45-parts.pdf', '_blank');
window.open('/sites/default/files/manuals/flexer-xl-parts.pdf', '_blank');

} else if (classic.is(':checked')) {
window.open('/sites/default/files/manuals/classic-xl-parts.pdf', '_blank');

} else if (racing.is(':checked')) {
window.open('/sites/default/files/manuals/racing-45-parts.pdf', '_blank');

} else if (flexer.is(':checked')) {
window.open('/sites/default/files/manuals/flexer-xl-parts.pdf', '_blank');

} else {
alert("Please choose a file to download");
}
});
});


If someone could spot what I'm doing wrong here, it would be greatly appreciated!

Answer

You are using if / else if. You need to use just if statements. After the first true if or else if statement, all else if and else statements are skipped, so only one code block is executed in an if / else if / else structure.