ovod
ovod

Reputation: 1178

JSP/HTML required fields only for specific submit button

I have table for user with 8 fields to complete fields via input type="text". I have added to 4 fields tag "required". And 4 buttons input type="submit":

add
delete
edit
deactivate

The problem is that "required" field is checked for all submit buttons, but I want it to be checked only when "add" button is pushed. Is there any way to bind "required" field to specific button?

Upvotes: 0

Views: 1622

Answers (1)

Gurkan Yesilyurt
Gurkan Yesilyurt

Reputation: 2675

Try it please.

$("form input[type=submit]").click(function() {
   //console.log($(this).val());
   if($(this).val()!=="ADD"){
      $('form input[type=text]').each(function() {
         $(this).removeAttr("required");
      });
   }
   $("input[type=submit]", $(this).parents("form")).removeAttr("clicked");
   $(this).attr("clicked", "true");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action='nothing' method='post' name='testform'>
    <input type="text" id="name" required>
    <input type="text" id="age" required>
    <input type='submit' name='add' value='ADD' />
    <input type='submit' name='delete' value='DELETE' />
</form>

Upvotes: 0

Related Questions