How to get input through a popup and put text in a variable through javascript / jquery

I have a button on my page. When clicked, a popup window appears allowing the user to enter text. When OK / Submit is clicked, my jscript then performs some functions using this input. Very simple, but I just can't figure out how to do this.

Thanks!

+6
source share
2 answers

it has the simplest form, you can use the invitation (question, by default) : (taken from w3schools: http://www.w3schools.com/js/tryit.asp?filename=tryjs_prompt )

function myFunction(){ var x; var name=prompt("Please enter your name","Harry Potter"); if (name!=null){ x="Hello " + name + "! How are you today?"; alert(x); } } 

anything else will require a lot of javascript and CSS to create layers with buttons and click events on those buttons

+16
source

Paste this code inside head tags between script tags

HTML

 <button id="button">Get Text</button>​ 

Js

 window.onload=function() { var el=document.getElementById('button'); el.onclick=function(){ var my_text=prompt('Enter text here'); if(my_text) alert(my_text); // for example I've made an alert } } 

Demo.

0
source

Source: https://habr.com/ru/post/919853/


All Articles