zankudo zankudo - 1 year ago 66
Javascript Question

Javascript trigger request with a button?

I am learning JavaScript, I have reached a section called "creating objects", since I am a novice in programming I cant figure out how to trigger the "prompt" request by using a button, instead of being triggered when the page loads, any idea?

myobjt = new Object();
var reqPrompt = prompt("Type a word:");
myobjt.mnfstInput = reqPrompt;
function mFunc() {
myobjt.mnfstScreen = mFunc;

Answer Source

While both of the other answers are correct, and are a very good way to do it, I'll try to answer using an object

var myObject = {
     input: "",
     popup: function(){
         alert("Hello, " + this.input);
     setInput: function(){
         this.input = prompt("Enter Name");
 <button type="button" onclick="myObject.setInput()">Input</button>
 <button type="button" onclick="myObject.popup()">Output</button>


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download