Reputation: 14107
I'm trying to create ONE javascript function that can do the following:
You can say I'm trying to create my own version of javascript prompt.
The problem is how to do action #2?
Any help appreciated.
Upvotes: 2
Views: 3658
Reputation: 488374
Any reason for the emphasis of ONE Javascript function?
function showModal() {
var div = document.createElement('div');
div.id = 'myprompt';
div.innerHTML = '<form id="test" onsubmit="return handleModal();"><input type="text" name="first"></form>';
document.body.appendChild(div);
}
function handleModal() {
alert('Hello, ' + document.getElementById('test').first.value);
return false;
}
showModal();
Check this out in action. Just type something and press enter. Styling would be done via CSS.
A big part of Javascript is events: do this when this happens. You shouldn't try to fight it.
As you can see, this gets hairy fast. You should look into the many, many, libraries and plugins that handle this very nicely. I personally recommend jQuery and Thickbox, jModal or Impromptu.
Upvotes: 0