1

The plan is to set id #required as required field on click. This does not work:

 $("#submitformat").click(function( event ) {
 $("#required").prop('required',true);
 event.preventDefault();
 });
chridam
  • 100,957
  • 23
  • 236
  • 235
slc
  • 79
  • 8

3 Answers3

3

I think set required attribute is not good thing if you wan't to make any field to required then please see below J Query code

$("#submitformat").click(function(event) {
   var test=$("#required").val();
   if(test==''){
    event.preventDefault();
   }
 });
Yogesh Sharma
  • 2,017
  • 1
  • 15
  • 33
1
$("#submitformat").click(function(event) {
     event.preventDefault();
     $('#required').attr('required', 'required');
     return false;
});
num8er
  • 18,604
  • 3
  • 43
  • 57
  • Using this even if #required is populated submit click does nothing – slc Jun 19 '14 at 13:42
  • we don't understand why to do that trick? when You could just add element with required="required". so You wanted to add this param and I gave You resolution. :) – num8er Jun 19 '14 at 13:46
  • @num8er exactly :) accepted answer checks if there's required attr and cancels event, it doesn't even cancel immediate propagation :) What was asked is to add required attribute on click. – Matas Vaitkevicius Jun 19 '14 at 15:24
0

I would suggest returning false, it will prevent event from bubbling up the DOM tree, and required attribute is used like 'required' = 'required' and not 'required' = 'true' . It might work on some browsers, but not all, also you should apply binding when document has fully loaded, best on document.ready.

$(function(){
  $("#submitformat").click(function() {
     $("#required").prop('required','required');
     return false;
      });
 });
Community
  • 1
  • 1
Matas Vaitkevicius
  • 58,075
  • 31
  • 238
  • 265