Page 1 of 1

Modifying the value of text input with js/ jquery

Posted: 18 Dec 2014, 15:36
by Marius
How can I dynamically set /modify the values of certain form elements, more specifically certain input text fields? So far, whenever I load my html page, I am just getting the blank input field, but I want to add it the value of 1.
Here is an example of how I am trying to do this.

Code: Select all

<!doctype html>
<html>
<head>
<script type="text/javascript" src="javascript/jquery-1.6.1.min.js"></script>
</head>
<body>
<form id="form1">
  <input type="text" name="q01" maxlength="1" />
</form>

<script type="text/javascript">
// here what JS /jQuery code so to set /modify value of #q01 ?
</script>
</body>
</html>
The reason this needs to be done dynamically is because the value of form could be different every time.

Modifying the value of text input with js/ jquery

Posted: 18 Dec 2014, 15:50
by MarPlo
Hi,
Add an ID to the input text field, for example:

Code: Select all

<form id="form1">
  <input type="text" name="q01" id="id_i1" maxlength="1" />
</form>
And the code.
- With pure JavaScript:

Code: Select all

<script>
document.getElementById('id_i1').value = 'The value';
</script>
- With jQuery:

Code: Select all

<script>
$('#id_i1').val('The value');
</script>