Sweetalert: Input returns empty string if defaultValue not changed

Created on 8 Nov 2017  路  7Comments  路  Source: t4t5/sweetalert

A swal with an input element will return an empty string if the original value is unchanged. For example:

swal({
  text: 'Enter some text',
  content: {
    element: 'input',
    attributes: {
      value: 'hello'
    }
  },
buttons: [true, true]
}).then((retVal) => {
  console.log(retVal)
})

The value of retVal is

  • null if you hit cancel
  • "" if you hit okay
  • "somestring" if you actually change the input
  • "" if you remove all the text and submit an empty string

It should be "hello" if you hit okay, not an empty string. There is no way to determine if someone removed all the text and wants to submit an empty string, or if they submitted the defaultValue/value.

I have tried changing value to defaultValue and that has no effect.

I believe this is because the input element is only calling setActionValue on change, and it on init it should be checking if it has a defaultValue, and then calling setActionValue(this.defaultValue)

Most helpful comment

The issue is still existed in the latest stable version 2.1.2.

All 7 comments

Sounds like a bug, and your analysis seems right. Feel free to send a pull request fixing this if you have time!

The issue is still existed in the latest stable version 2.1.2.

The issue still exists on version 2.1.2.

This fix was backed out via #774 for some reason.

The same error

Same error as well

Anyone have a solution to this? I still get the same issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yrshaikh picture yrshaikh  路  4Comments

Untit1ed picture Untit1ed  路  5Comments

Lusitaniae picture Lusitaniae  路  4Comments

vmitchell85 picture vmitchell85  路  6Comments

daftspunk picture daftspunk  路  4Comments