How to take in text input from a keyboard and store it into a variable?

前端 未结 7 2105
清酒与你
清酒与你 2020-12-30 05:43

I would just like something simple to read text from a keyboard and store it into a variable. So for:

var color = \'blue\'

I would like the

7条回答
  •  太阳男子
    2020-12-30 06:16

    If I understood your need, that should do it:

    html:

    
    

    javascript:

    function setValue(){
       color=document.getElementById("userInput").value;
       //do something with color
    }
    

    if you don't need to do something everytime the input changes, you can just get the input whenever you want to do something with 'color':

    html:

    
    

    javascript:

    color=document.getElementById("userInput").value;
    

提交回复
热议问题