简体   繁体   English

如何在TextArea中显示滑块值

[英]How to Display Slider Value In The TextArea

Hi I would like to display the sliders selected value displays in the text are when button is pressed. 嗨,我想显示滑块选择值显示在文本中,当按下按钮时。

The code needs to be pure javascript 该代码必须是纯JavaScript

i have manage to display the selected data in the console but i have no idea about how to display that data into text area. 我设法在控制台中显示选定的数据,但我不知道如何将数据显示到文本区域。

 function execute() { var textsize = document.getElementById("myRange").value; console.log(textsize); } 
 <button type="button" onclick="execute();">Button</button> <div id="textsizeslider" class="slidecontainer"> Text Size: <input type="range" min="10" max="40" value="20" class="slider" id="myRange"> </div> <textarea name="printerInstructions" id="printerInstructions" rows="8"></textarea> 

To do this you simply need to refer to your textbox in your javascript code. 为此,您只需要在javascript代码中引用您的文本框即可。

You can do this by doing 你可以这样做

document.getElementById('printerInstructions')

This will refer to your textbox/textarea in your HTML. 这将引用HTML中的文本框/文本区域。 Thus, to change its value, you can use its .value attribute and set it equal to the textsize . 因此,要更改其值,可以使用其.value属性并将其设置为等于textsize

See working example below: 请参见下面的工作示例:

 function execute() { var textsize = document.getElementById("myRange").value; document.getElementById('printerInstructions').value = textsize; } 
 <button type="button" onclick="execute();">Button</button> </div> <div id="textsizeslider" class="slidecontainer"> Text Size: <input type="range" min="10" max="40" value="20" class="slider" id="myRange"> </div> <textarea name="printerInstructions" id="printerInstructions" rows="8"></textarea> 

For "live" changes to your textbox as you slide you can add the oninput event to your slider to trigger your execute function everytime you slide your slider: 为了在滑动时对文本框进行“实时”更改,您可以在滑动器上添加oninput事件,以在每次滑动时触发execute功能:

 function execute(textsize) { document.getElementById('printerInstructions').value = textsize; } 
 <div id="textsizeslider" class="slidecontainer"> Text Size: <input oninput="execute(this.value)" type="range" min="10" max="40" value="20" class="slider" id="myRange"> </div> <textarea name="printerInstructions" id="printerInstructions" rows="8"></textarea> 

Just equate the textarea value to the slider value 只需将textarea值等于滑块值

 function execute() { var textsize = document.getElementById("myRange").value; document.getElementById("printerInstructions").value=textsize } 
 <button type="button" onclick="execute();">Button</button> </div> <div id="textsizeslider" class="slidecontainer"> Text Size: <input type="range" min="10" max="40" value="20" class="slider" id="myRange"> </div> <textarea name="printerInstructions" id="printerInstructions" rows="8"> </textarea> 

This is in addition to the answers provided . 这是除了答案 提供 If you need to see the change the moment it is updated, you can use event handler and update it. 如果您需要在更新时立即查看更改,则可以使用事件处理程序进行更新。

 document.getElementById('printerInstructions').value = document.getElementById("myRange").value; document.getElementById("myRange").addEventListener('change', () => { document.getElementById('printerInstructions').value = document.getElementById("myRange").value; }); 
 <div id="textsizeslider" class="slidecontainer"> Text Size: <input type="range" min="10" max="40" value="20" class="slider" id="myRange"> </div> <textarea name="printerInstructions" id="printerInstructions" rows="8"></textarea> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM