How to add the ‘required’ attribute to input box using jquery

The required attribute is a boolean attribute.

When present, it specifies that an input field must be filled out before submitting the form to create dynamic required attribute to see below example.

Note: The required attribute works with the following input types: text, search, url, tel, email, password, date pickers, number, checkbox, radio, and file.

Some PHP Code using ajax to response html page and jquery generate dynamic required attribute/

$i = 0;
while ($row = mysqli_fetch_assoc($query))
{
	$html.='   
	<tr>
		<td style="text-transform: capitalize;">'.$row['document_name'].'</td>
		<td width="10%">
			<input  type="file" id='.$i.' class="requireddocument"  name="carproof'.$i.'" ></td>
		<td>
		<input id="requireddoc'.$i.'" type="text" class="form-control datepicker" name="expiry_date_car'.$i.'" >
	</tr>';
	$i++;
}

Jquery change event

$('body').delegate('.requireddocument','change',function(){
	 var id = $(this).attr('id');
	 $('#requireddoc'+id).prop('required',true);
});

Leave a Reply

Your email address will not be published. Required fields are marked *