<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css">
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<fieldset id="realButton">
<legend>What would happen if we used real button</legend>
<input type="text" />
<button type="button" disabled="disabled" class="ui-button ui-widget ui-button-text-only"><span class="ui-button-text">Click me !</span></button>
</fieldset>
<fieldset id="jQueryButton">
<legend>What happens with a jQuery button</legend>
<input type="text" />
<ul>
<li>Click me !</li>
</ul>
</fieldset>
<fieldset id="jQueryHack">
<legend>What can happen using a hack for browsers that support capturing event</legend>
<input type="text" />
<ul>
<li>Click me !</li>
</ul>
<input type="text" class="keydump" />
</fieldset>
/*
The real button shows the normal behaviour if we would not be using jQuery's button. We should be able to get this exact behaviour.
*/
$("#realButton").children('button').click(function() {
alert('Hello ' + $(this).closest('fieldset').children('input:first').val() + ' !');
});
$('#realButton').children('input').change(function() {
var self = $(this);
if (self.val()) {
self.closest('fieldset').find('.ui-button').removeAttr('disabled');
}
else {
self.closest('fieldset').find('.ui-button').attr('disabled', 'disabled');
}
});
/*
In this case, we use a jQuery button based on a li element. The change event is not triggered before the button click which cause an odd behaviour.
*/
$('#jQueryButton').find('li').button({
disabled: true
}).click(function() {
alert('Hello ' + $(this).closest('fieldset').children('input:first').val() + ' !');
});
$('#jQueryButton').children('input').change(function() {
var self = $(this);
self.closest('fieldset').find('.ui-button').button(self.val() ? 'enable' : 'disable');
});
/*
This final example use event capturing to relocate the focus manually before the click event is propagated to the target button. This forces the input to process its change event.
*/
$('#jQueryHack').find('li').button({
disabled: true
}).click(function() {
alert('Hello ' + $(this).closest('fieldset').children('input:first').val() + ' !');
});
$('#jQueryHack').children('input').change(function() {
var self = $(this);
self.closest('fieldset').find('.ui-button').button(self.val() ? 'enable' : 'disable');
});
$('#jQueryHack').children('ul')[0].addEventListener('click', function() {
$('.keydump')[0].focus();
}, true);
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.