jQuery add required to input fields

Miura-shi picture Miura-shi · Oct 3, 2013 · Viewed 437.8k times · Source

I have been searching ways to have jQuery automatically write required using html5 validation to my all of my input fields but I am having trouble telling it where to write it.

I want to take this

 <input type="text" name="first_name" value="" id="freeform_first_name"
 maxlength="150">

and have it automatically add required before the closing tag

 <input type="text" name="first_name" value="" id="freeform_first_name"
 maxlength="150" required>

I thought I could do someting along the lines of

$("input").attr("required", "true");

But it doesn't work. Any help is greatly appreciated.

Answer

Unknown picture Unknown · Oct 3, 2013
$("input").prop('required',true);

DEMO FIDDLE