Skip to content

Instantly share code, notes, and snippets.

@khalid-s
Created October 31, 2016 20:21
Show Gist options
  • Select an option

  • Save khalid-s/3c175243f8a3f25582acd2dc8df100ea to your computer and use it in GitHub Desktop.

Select an option

Save khalid-s/3c175243f8a3f25582acd2dc8df100ea to your computer and use it in GitHub Desktop.
Styling input file, radio and checkbox form elements. Just need to use some CSS
/**
* Created by iKNSA.
* Author: Khalid Sookia <khalidsookia@gmail.com>
* Date: 31/10/16
* Time: 20:36
*/
'use strict';
var IKNSA_STYLE_INPUT = {
getInputSubstitute: function (element, uniqId) {
if (element.context.type === 'file') {
return '<span class="iknsa-style-substitute iknsa-style-' + element.context.type + '-substitute" data-uniq-id="' + uniqId + '"><span class="iknsa-special-file-label">' + element.attr('data-iknsa-style-file-placeholder') + '</span><span class="iknsa-special-file-button">' + element.attr('data-iknsa-style-file-button') + '</span></span>';
}
return '<span class="iknsa-style-substitute iknsa-style-' + element.context.type + '-substitute" data-uniq-id="' + uniqId + '"></span>';
},
addInputSubstitute: function (element, uniqId) {
var substitute = IKNSA_STYLE_INPUT.getInputSubstitute(element, uniqId);
element.after(substitute);
element.hide();
},
addInputUniqId: function(element, uniqId) {
element.attr('data-uniq-id', uniqId);
},
syncInputAndSubstitute: function () {
$.each($('.iknsa-style'), function () {
var substitute = $('.iknsa-style-substitute[data-uniq-id=' + $(this).attr('data-uniq-id') + ']');
if ($(this).is(':checked')) {
substitute.addClass('active');
} else {
substitute.removeClass('active');
}
});
},
bindSubstituteClickToInput: function (input) {
var substitute = $('.iknsa-style-substitute[data-uniq-id=' + $(input).attr('data-uniq-id') + ']');
substitute.click(function (event) {
$(input).trigger('click', event);
$('body').trigger('iknsaSpecialInputChanged');
});
},
manageSpecialInput: function () {
$('body').on('iknsaSpecialInputChanged', function() {
IKNSA_STYLE_INPUT.syncInputAndSubstitute();
});
$.each($('.iknsa-style'), function () {
var uniqId = UTILITIES.getUniqId();
IKNSA_STYLE_INPUT.addInputSubstitute($(this), uniqId);
IKNSA_STYLE_INPUT.addInputUniqId($(this), uniqId);
IKNSA_STYLE_INPUT.syncInputAndSubstitute(null, $(this));
IKNSA_STYLE_INPUT.bindSubstituteClickToInput($(this));
$(this).change(function () {
$('body').trigger('iknsaSpecialInputChanged');
});
});
},
init: function () {
if($('.iknsa-style').length > 0) {
IKNSA_STYLE_INPUT.manageSpecialInput();
}
}
}
IKNSA_STYLE_INPUT.init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment