5

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.

void
  • 34,922
  • 8
  • 55
  • 102
flik
  • 3,135
  • 2
  • 17
  • 29

3 Answers3

5

value is a property and not a method.

document.getElementById('txt1').value = 'anyvalue1111';
void
  • 34,922
  • 8
  • 55
  • 102
4

Try this

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

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

    </script>
Tibin
  • 582
  • 1
  • 6
  • 21
1

This syntax is for jQuery:

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

For Javascript use:

document.getElementById('txt1').value = 'anyvalue1111';
Muhammad Yasin
  • 393
  • 3
  • 11