Fir...">

How to set value in JavaScript entry?

I have an input field with id txt1, but I cannot change the value from JavaScript.

<form action=""> 
First name: <input type="text" id="txt1" onkeyup="showHint(this.value)">
</form>

<script>
  document.getElementById('txt1').value('anyvalue1111');
</script>

Note . I found that in stackoverflow how to change the input value, but did not find the answer. The title of the question saves a lot of time. This is a fair question in this way.

+4
source share
3 answers

This syntax is for jQuery:

$('#txt1').val('anyvalue1111');

To use Javascript:

document.getElementById('txt1').value = 'anyvalue1111';
+1
source

value is a property, not a method.

document.getElementById('txt1').value = 'anyvalue1111';
+4
source

try it

<form action=""> 
    First name: <input type="text" id="txt1" onkeyup="showHint(this.value)">
    </form>

    <script>
    document.getElementById('txt1').value = 'Hello world!!';

    </script>
+2
source

Source: https://habr.com/ru/post/1694567/


All Articles