12

I have a button on my page. When clicked a popup box should appear allowing the user to enter text. When OK/Submit is pressed, my jscript will then perform some functions using that inputted data. Very straightforward, but I just can't figure out how to do this.

Thanks!

2 Answers 2

37

in it's simplest form, you could use prompt(question, 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 would require lots of javascript & CSS to create layers with buttons and click events on those buttons

2
  • Awesome, works perfectly, but I was wondering if its possible to make this box bigger, like a squarish text input box?
    – Mark
    Jul 6, 2012 at 14:21
  • 1
    nope, it's a browser specific window. Like i said, you'll have to use javascript/CSS to create boxes like that. there's probably libraries available to use for this, like jQuery UI jqueryui.com/demos/dialog Jul 9, 2012 at 13:27
0

Paste this code inside your 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.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.