I have an HTML form
<div class="field">
<label for="num1">Number 1</label>
<input id="num1" type="text" />
</div>
<div class="field">
<label for="num2">Number 2</label>
<input id="num2" type="text" />
</div>
When “Number 1” is changed, I’d like to change “Number 2” to match it. So this JQuery code:
$('#num1').change(function() {
var one = this.val();
$('#num2').val(one);
});
but it says:
Uncaught TypeError: Object #<HTMLInputElement> has no method 'val'
on the line
var one = this.val()
What am I doing wrong?
.val()is only defined on jQuery objects.thisreturns the DOM object, which does not have this property.To fix your code:
$(this).val(), orthis.value