0

I have this code:

$(document).on('submit','#roomsend',function(e) {
   var form = $('#roomsend');
   var data = form.serialize();
   $.post('php/roomlist.php', data, function(response) {
      console.log(response);
      $('#power').replaceWith(response);
   });
   return false;
});

This works with a Submit button inside the form. But now I have a button outside the form:

<button class="btn btn-success" id="roomsendbutton"><i class="icon-ok"></i> Save</button>

How must I must be the code, that when i clicked the button the button all actions will be work and the form submits?

1
  • $(document).on('click','#roomsendbutton',function(e){..}) solves your problem i guess Commented May 24, 2013 at 19:35

4 Answers 4

4

If you're using HTML5, you can use the new form attribute:

<button form="roomsend" type="submit" class="btn btn-success" id="roomsendbutton"><i class="icon-ok"></i> Save</button>

http://jsfiddle.net/sEHLf/

Should probably look up browser support for it though.

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

1 Comment

If only it were supported more broadly. It appears as though IE10 doesn't support it.
2

Use .trigger() method:

$('#roomsendbutton').on('click', function () {
    $('#roomsend').trigger('submit');
});

References:

1 Comment

Sorry but it didn't work. I want also no submit button in the form but only these who is outside the form .
0

Just do something like this in your jQuery..

$('#roomsendbutton').click(function(){
     $(your form name).trigger('submit');

});

1 Comment

Sorry but it didn't work. I want also no submit button in the form but only these who is outside the form .
0
$(document).on('click','#roomsendbutton',function(e) {
   var form = $('#roomsend');
   var data = form.serialize();
   $.post('php/roomlist.php', data, function(response) {
      console.log(response);
      $('#power').replaceWith(response);
   });
   return false;
});

Just bind the click event on the button.

3 Comments

But then the form will submit without hitting this handler when the user presses enter on a form input.
Well if the button is out of the form, pressing enter won't do anything. And nothing prevents you from binding both event if needed ;)
If the button is not in the form, and the input is, pressing enter on the input will submit the form. But yes, you could bind additional events to fix that too.

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.