Created
March 26, 2013 13:32
-
-
Save ismaels/5245376 to your computer and use it in GitHub Desktop.
Javascript function to toggle an element based on other elements property
This file contains hidden or 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
//author: Ismael Stahelin | |
//date: 25.3.2013 | |
//the toggle function | |
//is based on selected option of a select element. if selected option has the desired property with the desired value | |
//so another element, in this case a checkbox element is shown, otherwise it is hidden | |
var toggleTemperature = function(){ | |
var op = $('#tracking_module_tracking_module_model_id option:selected'); | |
if($(op).attr('data-has_temperature_sensor') == 'true'){ | |
$('#temperature_control').show("fast"); | |
}else{ | |
$('#temperature_control').hide('fast'); | |
$('#use_temperature_sensor').prop('checked', false); | |
} | |
}; | |
//call to handle the toggle of element on page load | |
toggleTemperature(); | |
//register the toggle function to be called when a change occurs on the select element | |
$('#tracking_module_tracking_module_model_id').change(function(){ | |
toggleTemperature(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment