3

Given the following form:

<form action method="post" onsubmit="func(this)"> <button type="submit" value="prev" >prev</button> <button type="submit" value="next" >next</button> <button type="submit" value="submit">submit</button> <button type="reset" value="reset" >reset</button> </form> 

Within func(), how to identify which button submitted the form?

8
  • what javascript function? Commented Sep 4, 2015 at 10:30
  • Java script function is used to validate the fields based on the button clicked. if button clicked is next then different validation etc Commented Sep 4, 2015 at 10:34
  • js function or event can be fired and then you can identify which button is pressed Commented Sep 4, 2015 at 10:38
  • 1
    Can you show what you try so far ? Edit the question. Commented Sep 4, 2015 at 10:48
  • 1
    stackoverflow.com/questions/2066162/… Commented Sep 6, 2015 at 13:56

1 Answer 1

2

You are doing it wrong. You can attach click event to each button and take action. This is cleanest way.

<button onclick="alert('You are clicking on me');">Reset</button> 

Instead of alert insert your custom function. You can make it more robust by not using inline javascript. In those functions you can choose if you want to submit form or not by preventing or passing default event. Like event.preventDefault()

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @Mateusz, its working fine. I used the solution suggested by you.
@ashu then you can accept answer, no need to comment ;-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.