t4t5 / sweetalert

A beautiful replacement for JavaScript's "alert"
https://sweetalert.js.org
MIT License
22.4k stars 2.84k forks source link

Attribute value for the input doesn't work correctly. #809

Open wondertalik opened 6 years ago

wondertalik commented 6 years ago
    swal({
        title: "Separar las adreces de correu electrònic amb coma",
        //dangerMode: true,
        buttons: ["Cancel · lar", "Sí"],
        content: {
            element: "input",
            attributes: {
                placeholder: "Correus",
                type: "text",
                value: `${self.cliente.email}`
            }
        }

    })
        .then(function (value) {
            console.error(value);
        })

screen shot 2018-03-05 at 11 22 11 am

Hi, all. Awesome library. But have a problem with default value in the input. If i press cancel - value is null, if si - value is empty. if i did some changes in the input get correct value.

sf-jd commented 6 years ago

Not sure if this addresses your current problem but wherever you're triggering this swal:

let email = self.cliente.email
swal({
    title: "Separar las adreces de correu electrònic amb coma",
    buttons: {
        cancelar: {
            text: "Cancel · lar",
            value: "something other than null"
        },
        si: {
            text: "Si",
            value: email
        }
    },
    content: {
        element: "input",
        attributes: {
            placeholder: "Correus",
            type: "text",
            value: email
        }
    }

})
.then((value) => {
    console.log(document.getElementById('input_mail').value)
    console.error(value);
})

The flow basically sends the value of the default email, but if the user changes the input's value, then you can grab it in the then clause by targetting the input element. Dirty, i know, but might help you get by. Also, if you dont have a problem with the default cancel being null, you can always deal with it in the then by checking for null and doing something about it :)

kaddi220 commented 5 years ago

I have the same problem. if there are no changes to the input field, default value is empty

VlLight commented 5 years ago

Same for me too. Sweet Alert returns empty value if I don't make any change in default input field value

djlxiaoshi commented 4 years ago

the same as me

quartertone commented 4 years ago

This issues is still a big problem. How is it not resolved already?

document.getElementById("btn").onclick = function() {
 swal("Test default value", {
   buttons: {
    cancel: true,
    ok: {
     value: "Button value"
    }
   },
   content: {
    element: "input",
    attributes: {
     value: "Default value"
    }
   }
  })
  .then((result) => {
   console.log(result);
  });
}

Expected behavior. If the input text is changed, the new value should be passed as the result. If the input text is unchanged, the default value should be passed as the result.

What happens: 1 - If input text is changed, and [Enter] button is pressed, the new value is passed as expected. This is ok.

2 - If the [Enter] button is pressed without making any changes, NULL value is passed.

3 - If the [Ok] button is pressed/clicked, the BUTTON value gets passed regardless of whether the input text was changed or not.

Please fix this. Otherwise this library is kind of useless.

Also note that this problem occurs even with the official examples: See the second example under https://sweetalert.js.org/guides/#using-dom-nodes-as-content