231

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.

Ronak Shah
  • 355,584
  • 18
  • 123
  • 178
Miura-shi
  • 4,139
  • 4
  • 32
  • 53

6 Answers6

531
$("input").prop('required',true);

DEMO FIDDLE

Kiran
  • 19,739
  • 10
  • 64
  • 98
65

You can do it by using attr, the mistake that you made is that you put the true inside quotes. instead of that try this:

$("input").attr("required", true);
Joz Naveen Joz
  • 787
  • 6
  • 3
  • 2
    @JohnMeyer "input" is the name of a tag selector – dave4jr Feb 11 '17 at 23:09
  • `required` is a _boolean_ attribute and should only ever be omitted (for "false"), or have the same value as its name (i.e. `"required"`) for "true". It's actually better to use `.prop()`. – Alnitak Jan 10 '20 at 10:33
  • @Alnitak Not if you're working on making a site compatible with IE, `prop()` doesn't work on there as it does on modern browsers. Still best to use `$(ele).attr("required", true)` and `false` to remove. – Oliver Heward Jun 18 '20 at 15:09
41

I have found that the following implementations are effective:

$('#freeform_first_name').removeAttr('required');

$('#freeform_first_name').attr('required', 'required');

These commands (attr, removeAttr, prop) behave differently depending on the version of JQuery you are using. Please reference the documentation here: https://api.jquery.com/attr/

John Meyer
  • 2,106
  • 1
  • 28
  • 37
12

Using .attr method

.attr(attribute,value); // syntax

.attr("required", true);
// required="required"

.attr("required", false);
// 

Using .prop

.prop(property,value) // syntax

.prop("required", true);
// required=""

.prop("required", false);
//

Read more from here

https://stackoverflow.com/a/5876747/5413283

Dexter
  • 5,748
  • 3
  • 34
  • 32
7

Should not enclose true with double quote " " it should be like

$(document).ready(function() {            
   $('input').attr('required', true);   
});

Also you can use prop

jQuery(document).ready(function() {            
   $('input').prop('required', true);   
}); 

Instead of true you can try required. Such as

$('input').prop('required', 'required');
Rokan Nashp
  • 321
  • 4
  • 13
6

I found that jquery 1.11.1 does not do this reliably.

I used $('#estimate').attr('required', true) and $('#estimate').removeAttr('required').

Removing required was not reliable. It would sometimes leave the required attribute without value. Since required is a boolean attibute, its mere presence, without value, is seen by the browser as true.

This bug was intermittent, and I got tired of messing with it. Switched to document.getElementById("estimate").required = true and document.getElementById("estimate").required = false.

Paul Roub
  • 35,848
  • 27
  • 79
  • 88