5

I have a submit button #next_step and a link #design_next_step.

When the button submitted or the link is clicked, the same function is triggered.

How can I do something like this?

$('#next_step').submit(), $('#design_next_step').click(){function(){ ... });

1
  • if your button(input) type is submit, you just need to handle click event Commented Jun 22, 2013 at 10:43

6 Answers 6

6

You can use the standard CSS comma to define a group selector:

$('#next_step, #design_next_step').on('submit click', function(){ ... });

When the button submitted or the link is clicked...

But buttons aren't submitted, they're clicked. The submit event relates to form elements, not button or input elements. Assuming that the ids you've shown there are for elements that are buttons or links, just use the click event:

$('#next_step, #design_next_step').on('click', function(){ ... });

Depending on what you're doing, you may — or may not — want to prevent the default action for the event [by accepting the event argument and calling preventDefault on it, or by doing return false in the handler which will both prevent the default and stop propagation]. The default action for click on links is to follow the link, for instance.

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

2 Comments

Oh you're right...buttons aren't submitted. But what about if #next_step is input type='submit'. Isn't $('#next_step').submit() valid code?
@user961627: It's valid code, but it won't do anything. :-) The submit event is fired on form elements. When you click a submit button, the event fired on the button is click.
4

Just make both event handlers to call the same function F.

$('#next_step').submit(function() {
   F();
});
$('#design_next_step').click(function() {
   F();
});
var F=function() {
   . . .Write code here
}

Comments

2

you can give same class to both, the link as well as the button and then you can try doing following

$('.className').click(function() {
    var item = $(this); // item that triggered the event

});

and if you want to do based in IDs then following

$('#Button1, #Link1').click(function() {
    // You can use `this` to refer to the source element, for instance:
});

Comments

0

This would be more appropriate.

$('.anchor').click(function{
   $('form').submit();
});

Comments

0

I guess there is no way to do like that

Instead try to use same function in event handlers

$('#formId').submit(function{
   execute();
});

 $('#design_next_step').click(function{
 execute();
 })

4 Comments

Stop trying to submit buttons!
@Jonathan Sorry,I didn't get you.
You need to submit the form, the button can't
@Jonathan Oops..thanks for the spot Jonathan.My intention is that write one function and use multiple times :)
0

May be you can just call it like you would call a normal function.Since in this case submit and click perform the same action ,when the user submits or clicks the link,they call the funcion "myFunction"

                function myFunction()
                      {
                        ..........
                      }
                $('#button').submit(myFunction);
                $('#link').click(myFunction);

or

                 var myFunction=function()
                    {
                       .......
                    }

                  $('#button').submit(myFunction);
                  $('#link').click(myFunction);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.