0

how can you submit a form by bypassing the required attribute using button 2

like i have button 2 and button 1

i want to submit form by clicking button 2 and bypass required attribute

and both buttons must have type=submit

<form method="POST">
  <input name="try" required>
  <button type="submit">button 1</button>
  <button type="submit">button 2</button>
</form>
VXp
  • 11,598
  • 6
  • 31
  • 46
mich kent
  • 63
  • 1
  • 7

3 Answers3

0

Using some simple Javascript should allow you to use button two but ignore the required attribute assigned to the text field.

document.querySelectorAll('button')[1].addEventListener('click',function(e){
  e.preventDefault();
  this.parentNode.try.required=false;
  this.parentNode.submit();
})
<h2>submit test</h2>
        
<form method="POST">
  <input type="text" name="try" required />
  <button type="submit">button 1</button>
  <button type="submit">button 2</button>
</form>
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
0

You can use formnovalidate

<form method="POST">
  <input name="try" required>
  <button type="submit">button 1</button>
  <button type="submit" formnovalidate>button 2</button>
</form>
Akhil
  • 1,403
  • 7
  • 19
0

Try

<form method="POST">
  <input name="try" required>
  <button type="submit">button 1</button>
  <button type="submit" value="ignore" >button 2</button>
</form>

Nicholas Stom
  • 322
  • 1
  • 3
  • 10