Sweetalert 2 select dropdown and textarea at the same time










3















I am wondering if it possible to allow both select dropdown list and a input textarea within the same sweet alert.



For example: my select list have 4 options and the last option is others. I want when user choose others and it will display a text area for the user to enter. I am using similar to what sweetalert 2 give as shown below.



 swal(
title: 'Select field validation',
input: 'select',
inputOptions:
'apples': 'Apples',
'bananas': 'Bananas',
'grapes': 'Grapes',
'Others': 'Others'
,
inputPlaceholder: 'Select a fruit',
showCancelButton: true,
inputValidator: (value) =>
return new Promise((resolve) =>
if (value === 'oranges')
resolve()
else
resolve('You need to select oranges :)')

)

)

if (fruit)
swal('You selected: ' + fruit)










share|improve this question
























  • Have a look at stackoverflow.com/questions/31463649/…

    – Alireza Fattahi
    Nov 14 '18 at 4:36















3















I am wondering if it possible to allow both select dropdown list and a input textarea within the same sweet alert.



For example: my select list have 4 options and the last option is others. I want when user choose others and it will display a text area for the user to enter. I am using similar to what sweetalert 2 give as shown below.



 swal(
title: 'Select field validation',
input: 'select',
inputOptions:
'apples': 'Apples',
'bananas': 'Bananas',
'grapes': 'Grapes',
'Others': 'Others'
,
inputPlaceholder: 'Select a fruit',
showCancelButton: true,
inputValidator: (value) =>
return new Promise((resolve) =>
if (value === 'oranges')
resolve()
else
resolve('You need to select oranges :)')

)

)

if (fruit)
swal('You selected: ' + fruit)










share|improve this question
























  • Have a look at stackoverflow.com/questions/31463649/…

    – Alireza Fattahi
    Nov 14 '18 at 4:36













3












3








3








I am wondering if it possible to allow both select dropdown list and a input textarea within the same sweet alert.



For example: my select list have 4 options and the last option is others. I want when user choose others and it will display a text area for the user to enter. I am using similar to what sweetalert 2 give as shown below.



 swal(
title: 'Select field validation',
input: 'select',
inputOptions:
'apples': 'Apples',
'bananas': 'Bananas',
'grapes': 'Grapes',
'Others': 'Others'
,
inputPlaceholder: 'Select a fruit',
showCancelButton: true,
inputValidator: (value) =>
return new Promise((resolve) =>
if (value === 'oranges')
resolve()
else
resolve('You need to select oranges :)')

)

)

if (fruit)
swal('You selected: ' + fruit)










share|improve this question
















I am wondering if it possible to allow both select dropdown list and a input textarea within the same sweet alert.



For example: my select list have 4 options and the last option is others. I want when user choose others and it will display a text area for the user to enter. I am using similar to what sweetalert 2 give as shown below.



 swal(
title: 'Select field validation',
input: 'select',
inputOptions:
'apples': 'Apples',
'bananas': 'Bananas',
'grapes': 'Grapes',
'Others': 'Others'
,
inputPlaceholder: 'Select a fruit',
showCancelButton: true,
inputValidator: (value) =>
return new Promise((resolve) =>
if (value === 'oranges')
resolve()
else
resolve('You need to select oranges :)')

)

)

if (fruit)
swal('You selected: ' + fruit)







javascript sweetalert2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 4:22







nmm

















asked Nov 14 '18 at 4:15









nmmnmm

408




408












  • Have a look at stackoverflow.com/questions/31463649/…

    – Alireza Fattahi
    Nov 14 '18 at 4:36

















  • Have a look at stackoverflow.com/questions/31463649/…

    – Alireza Fattahi
    Nov 14 '18 at 4:36
















Have a look at stackoverflow.com/questions/31463649/…

– Alireza Fattahi
Nov 14 '18 at 4:36





Have a look at stackoverflow.com/questions/31463649/…

– Alireza Fattahi
Nov 14 '18 at 4:36












1 Answer
1






active

oldest

votes


















1














One possible way (if you want to keep using the input and inputValidator) is that you can use the html property of swal to add an hidden text input (e.g. using display: none):



html: '<input type="text" id="fruit-text" class="swal2-input" style="display: none;">',


Then you can use the onBeforeOpen to subscribe to change event on the input, e.g.:



onBeforeOpen: (dom) => 
swal.getInput().onchange = function (event)
if (event.target.value === "Others")
dom.querySelector('#fruit-text').style.display = 'initial'
else
dom.querySelector('#fruit-text').style.display = 'none'




,



An example of that can be found at https://south-carol.glitch.me/



Note that in this case, you should use the preConfirm to handle the status of the swal and return the value of the text input if needed (see source of the example at the url above)






share|improve this answer























  • thanks for your help :)

    – nmm
    Nov 15 '18 at 13:40











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293115%2fsweetalert-2-select-dropdown-and-textarea-at-the-same-time%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














One possible way (if you want to keep using the input and inputValidator) is that you can use the html property of swal to add an hidden text input (e.g. using display: none):



html: '<input type="text" id="fruit-text" class="swal2-input" style="display: none;">',


Then you can use the onBeforeOpen to subscribe to change event on the input, e.g.:



onBeforeOpen: (dom) => 
swal.getInput().onchange = function (event)
if (event.target.value === "Others")
dom.querySelector('#fruit-text').style.display = 'initial'
else
dom.querySelector('#fruit-text').style.display = 'none'




,



An example of that can be found at https://south-carol.glitch.me/



Note that in this case, you should use the preConfirm to handle the status of the swal and return the value of the text input if needed (see source of the example at the url above)






share|improve this answer























  • thanks for your help :)

    – nmm
    Nov 15 '18 at 13:40
















1














One possible way (if you want to keep using the input and inputValidator) is that you can use the html property of swal to add an hidden text input (e.g. using display: none):



html: '<input type="text" id="fruit-text" class="swal2-input" style="display: none;">',


Then you can use the onBeforeOpen to subscribe to change event on the input, e.g.:



onBeforeOpen: (dom) => 
swal.getInput().onchange = function (event)
if (event.target.value === "Others")
dom.querySelector('#fruit-text').style.display = 'initial'
else
dom.querySelector('#fruit-text').style.display = 'none'




,



An example of that can be found at https://south-carol.glitch.me/



Note that in this case, you should use the preConfirm to handle the status of the swal and return the value of the text input if needed (see source of the example at the url above)






share|improve this answer























  • thanks for your help :)

    – nmm
    Nov 15 '18 at 13:40














1












1








1







One possible way (if you want to keep using the input and inputValidator) is that you can use the html property of swal to add an hidden text input (e.g. using display: none):



html: '<input type="text" id="fruit-text" class="swal2-input" style="display: none;">',


Then you can use the onBeforeOpen to subscribe to change event on the input, e.g.:



onBeforeOpen: (dom) => 
swal.getInput().onchange = function (event)
if (event.target.value === "Others")
dom.querySelector('#fruit-text').style.display = 'initial'
else
dom.querySelector('#fruit-text').style.display = 'none'




,



An example of that can be found at https://south-carol.glitch.me/



Note that in this case, you should use the preConfirm to handle the status of the swal and return the value of the text input if needed (see source of the example at the url above)






share|improve this answer













One possible way (if you want to keep using the input and inputValidator) is that you can use the html property of swal to add an hidden text input (e.g. using display: none):



html: '<input type="text" id="fruit-text" class="swal2-input" style="display: none;">',


Then you can use the onBeforeOpen to subscribe to change event on the input, e.g.:



onBeforeOpen: (dom) => 
swal.getInput().onchange = function (event)
if (event.target.value === "Others")
dom.querySelector('#fruit-text').style.display = 'initial'
else
dom.querySelector('#fruit-text').style.display = 'none'




,



An example of that can be found at https://south-carol.glitch.me/



Note that in this case, you should use the preConfirm to handle the status of the swal and return the value of the text input if needed (see source of the example at the url above)







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 19:01









G. VerniG. Verni

3025




3025












  • thanks for your help :)

    – nmm
    Nov 15 '18 at 13:40


















  • thanks for your help :)

    – nmm
    Nov 15 '18 at 13:40

















thanks for your help :)

– nmm
Nov 15 '18 at 13:40






thanks for your help :)

– nmm
Nov 15 '18 at 13:40


















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293115%2fsweetalert-2-select-dropdown-and-textarea-at-the-same-time%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto