简体   繁体   中英

sweet alert input to js variable

I want to have a sweet alert input to ask the user to enter a value. Then I want to save the value as a JS variable, to be used later.

let coinName = swal("Enter cryptocurrency name", {content: "input"})
console.log(coinName)

Here is my full code:

function getPrices() {

let coinName = swal("Enter cryptocurrency name", {content: "input"})
console.log(coinName.value)


$.getJSON('http://www.whateverorigin.org/get?url=' + encodeURIComponent('https://api.coinmarketcap.com/v1/ticker/')+ coinName.valueOf + '/' + '&callback=?', function (data) {
    console.log(data.contents);
})


    var coinPrice = data.contents[0].price_usd
    swal('Currently, in USD, 1 ' + coinName + ' is equal to $' + coinPrice)


}

This what I want to do:

  1. Ask user for input.
  2. Take input and convert in into JS variable.

I'm not sure if I've explained this correctly, but any ideas or suggestions would be highly appreciated.

Based on the docs https://sweetalert.js.org/guides/#using-dom-nodes-as-content you can do this with

swal("Write something here:", {
  content: "input",
})
.then((value) => {
  // do something with value variable
});

If we update your code

function getPrices() {
    swal("Enter cryptocurrency name", {content: "input"}).then(function(coinName) {
        $.getJSON('http://www.whateverorigin.org/get?url=' + encodeURIComponent('https://api.coinmarketcap.com/v1/ticker/')+ coinName + '/' + '&callback=?', function (data) {
            console.log(data.contents);

            var coinPrice = data.contents[0].price_usd
            swal('Currently, in USD, 1 ' + coinName + ' is equal to $' + coinPrice)
        })
    }
}  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM