I have a form with 2 input fields and i want to add atribute require to the second input if the first field is completed and to remove the require attribute if this field is empty. This is the form:
<form id="someid">
<input type="text" id="input1">
<input type="text" id="input2">
</form>
Bellow is the code that i have tested
<script type="text/javascript">
$("#input1").on("keyup change", function() {
var value = this.value;
if ($("#input1:contains('" + value + "')").length > 1) {
$('#input2').addAttr('required');
}
else {
$('#input2').removeAttr('required');
}
});
</script>
Please test this:
Update:
$(document).on("keyup change", "#input1", function() {
if (this.value.length > 1) {
$('#input2').attr('required', true);
}
else {
$('#input2').removeAttr('required');
}
});