Last active
August 29, 2015 14:07
-
-
Save heathdutton/943b59be9f3844a202c6 to your computer and use it in GitHub Desktop.
Limit schools based on a state field in Hubspot.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (typeof jQuery == 'undefined'){ | |
console.log('Filterizer: Cannot be ran. jQuery is not loaded yet.'); | |
} else { | |
(function($) { | |
$(document).ready(function(){ | |
var attempt_time = 10, // amount of time to keep trying in seconds | |
interval_ms = 150, // how frequently to attempt in ms | |
elapsed_time = 0, // elapsed time in ms | |
interval = setInterval(function(){ | |
elapsed_time += interval_ms; | |
if (elapsed_time < (attempt_time * 1000)){ | |
var state_field = $('select[name=state]'), | |
schools_field = $('select[name=schools]'), | |
schools_options = schools_field.find('option'); | |
if (state_field.length && schools_field.length && schools_options.length) { | |
if (!state_field.hasClass('filterized')){ | |
state_field.change(function() { | |
schools_field.fadeTo(150, .2); | |
var state = $(this).val(), | |
school_count = schools_options.length; | |
setTimeout(function(){ | |
schools_options.each(function (index, element) { | |
var val = $(this).val(); | |
if ( | |
val.indexOf('(' + state + ')') != -1 || | |
val.indexOf('(NA)') != -1 || | |
val.indexOf('(NU)') != -1 || | |
val == 'My school is not listed' | |
) { | |
// This option should be visible | |
if ($(this).parent('span.toggleOption').length) { | |
$(this).unwrap(); | |
} | |
} else { | |
// This option should be hidden | |
if ($(this).parent('span.toggleOption').length == 0) { | |
$(this).wrap('<span class="toggleOption" style="display: none;" />'); | |
} | |
} | |
// Now that we are done, fade in the field to indicate we've done something. | |
if (index == school_count - 1) { | |
schools_field.stop().fadeTo(150, 1); | |
} | |
}); | |
}, 150); | |
}); | |
state_field.addClass('filterized'); | |
console.log('Filterizer: Ready.'); | |
clearInterval(interval); | |
} else { | |
console.log('Filterizer: Already ready.'); | |
clearInterval(interval); | |
} | |
} | |
} else { | |
console.log('Filterizer: Too many attempts, does not look like the form is coming up. Cancelling.'); | |
clearInterval(interval); | |
} | |
}, interval_ms); | |
}); | |
})(jQuery); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment