Skip to content

Instantly share code, notes, and snippets.

@vukcevich
Forked from postpostmodern/ie_button_fix.js
Created March 8, 2012 18:01
Show Gist options
  • Save vukcevich/2002375 to your computer and use it in GitHub Desktop.
Save vukcevich/2002375 to your computer and use it in GitHub Desktop.
// IE doesn't deal well with button elements.
// The following jQuery code fixes the two most common issues:
// 1. All button values being submitted whether they were clicked or not
// 2. Button labels being submitted instead of the value of the value attribute
// Make sure you have loaded jQuery, of course.
if ($.browser.msie) {
jQuery(function() {
$('form button[type=submit]').click(function() {
// Handle on the button clicked
var obj = $(this);
// Disable all buttons (keeps their values from being submitted)
$('button').attr('disabled', 'disabled');
// Get the text label of the button
var label = obj.text();
// Set it to an empty string so IE will read the real value attribute
obj.text('');
// Create a hidden field with the name and value of the button
obj.after($("<input/>").attr('type', 'hidden').attr('name', obj.attr('name')).val(obj.attr('value')));
// Restore the button's text label
obj.text(label);
// Submit the form
obj.closest('form').submit();
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment