Skip to content

Instantly share code, notes, and snippets.

@infynyxx
Created July 23, 2009 18:11
Show Gist options
  • Save infynyxx/153334 to your computer and use it in GitHub Desktop.
Save infynyxx/153334 to your computer and use it in GitHub Desktop.
JS custom event handling based on PrototypeJS
//Custom Event handling
Event.observe(document, 'dom:loaded', function() {
$('enableInputEvents').observe('click', function(e) {
document.fire('CheckBox:Enabled', {checked: $('enableInputEvents').checked});
});
});
document.observe('CheckBox:Enabled', function(e) {
Event.stop(e);
if (e.memo.checked) {
console.log("all elements with CSS class input are ENABLED");
$$('.input').each(function(t) {
t.observe('change', function() {
alert(t.value);
});
});
}
else {
console.log("all elements with CSS class input are DISABLED");
$$('.input').invoke('stopObserving');
}
});
/**
IMPLEMENTATION
<fieldset>
<legend>
JS Custom Events
</legend>
<div>
<input type="checkbox" value="1" id="enableInputEvents" />&nbsp;Enable events for text box and combo box<br/>
</div>
<div id="inputs_elements">
Text Box 1:&nbsp;<input type="text" id="textbox1" value="" class="input" /><br/>
Text Box 2:&nbsp;<input type="text" id="textbox2" value="" /><br/>
Select Search Engine: <select id="search-engine-selector" class="input">
<option value="">Go to</option>
<option value="http://www.google.com">Google</option>
<option value="http://www.yahoo.com">Yahoo!</option>
<option value="http://www.live.com">Live</option>
</select>
</div>
</fieldset>
**/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment