Created
July 30, 2014 19:53
-
-
Save danaskallman/0701883437d2caecb56d to your computer and use it in GitHub Desktop.
Sample JS for Event Custom Template v.02
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
{literal} | |
<script type="text/javascript"> | |
(function(glo, window, $ ){ | |
// set up a little namespace | |
if(!glo.ui) glo.ui = { | |
forms: {} | |
}; | |
// A field that is dependent on the value of a previous field. | |
glo.ui.forms.ConditionalField = function( targetSelector, triggerSelector, hideOrShowOnTrigger ){ | |
try { | |
$(triggerSelector).change( function(e){ | |
// e.preventDefault(); | |
console.log( "change" ); | |
if( $(triggerSelector).attr( 'checked' ) == 'checked' ){ | |
console.log("A"); | |
console.log( $(targetSelector) ); | |
$(targetSelector).hide(); | |
glo.ui.forms.clearChildValues( targetSelector ); | |
} else { | |
console.log("show it"); | |
$(targetSelector).show(); | |
} | |
}) | |
} catch (err) { | |
console.log( "Error", error ); | |
} | |
} | |
// Set the constructor | |
glo.ui.forms.ConditionalField.prototype.constructor = glo.ui.forms.ConditionalField; | |
// A static method that clears the input values within a selector | |
glo.ui.forms.clearChildValues = function( targetSelector ){ | |
// clears values of children | |
$(targetSelector).find( ":input" ) | |
.removeAttr('checked') | |
.removeAttr('selected') | |
.not(':button, :submit, :reset, :hidden, :radio, :checkbox').val(''); | |
} | |
})( window.glo = window.glo || {}, window, jQuery ); | |
// When the DOM is ready, do stuff… | |
(function($,window){ | |
$( document ).ready( function(){ | |
new glo.ui.forms.ConditionalField( ".Block_2-section", "input[name=price_29]" ); | |
}) | |
})( jQuery, window ); | |
</script> | |
{/literal} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment