How to add required attribute to input fields in jQuery?

I’m looking for a way to automatically add the “required” attribute to all of my input fields for HTML5 validation. However, I’m having trouble finding a way to use jQuery to dynamically add the “required” attribute to input fields.

Here is the HTML I have.

<input type="text" name="first_name" id="first-name">

You can achieve this in several ways, here are a few examples.



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


$('input').attr('required', 'required');

All of them will work.