Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set value in input by JavaScript?

I have input field with id txt1 but I am unable to 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 find on stackoverflow how to change input value but could not found any answer. Title of the question save lot of time. It is valid question in this way.

like image 419
flik Avatar asked Mar 07 '18 06:03

flik


2 Answers

value is a property and not a method.

document.getElementById('txt1').value = 'anyvalue1111';
like image 80
void Avatar answered Oct 13 '22 01:10

void


Try this

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

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

    </script>
like image 23
Tibin Avatar answered Oct 13 '22 02:10

Tibin