/*---------------------swal-forms----------------*/
;(function () {
// extend swal with a function for adding forms
swal.withForm = function () {
// initialize with field values supplied on `swal.withForm` call
var swalForm = new SwalForm(arguments[0].formFields)
// prevent successive calls to add duplicated form fields
swalForm.removeSwalForm()
// make form values inserted by the user available at `doneFunction`
swalForm.addWayToGetFormValuesInDoneFunction(arguments)
// forward arguments
swal.apply({}, arguments)
var htmlForm = swalForm.generateHtmlForm()
swalForm.insertFormInSwalModal(htmlForm)
swalForm.allowClickingDirectlyOnInputs()
swalForm.focusOnFirstInput()
swalForm.markFirstRadioButtons()
swalForm.addTabOrder()
}
// constructor for helper object
function SwalForm (formFields) {
this.formFields = formFields
}
// helper methods
extend(SwalForm.prototype, {
formClass: 'swal-form',
generateHtmlForm: function () {
var form = {
clazz: this.formClass,
innerHtml: this.formFields.map(toFormTag.bind(this)).reduce(toSingleString)
}
return t("<div class='{clazz}'>{innerHtml}</div>", form)
function toFormTag (field) {
var input = Input(field)
// to separate groups of checkboxes and radiobuttons in different lines
var conditionalLineBreak = (input.isRadioOrCheckbox() && this.lastFieldName !== field.name) ? '<br>' : ''
this.lastFieldName = field.name
return conditionalLineBreak + input.toHtml()
}
},
addWayToGetFormValuesInDoneFunction: function (swalArgs) {
var swalFormInstance = this
var doneFunction = swalArgs[1]
swalArgs[1] = function (isConfirm) {
// make form values available at `this` variable inside doneFunction
this.swalForm = swalFormInstance.getFormValues()
doneFunction.apply(this, arguments)
// clean form to not interfere in...
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.