1

I'm playing around in PHP, trying to make a simple calculator. I have made buttons that on click, add the value to the input field, like this;

My calculator

The jQuery:

$('.symbols button').click(function() {
    $('#advanced-sqrt').focus();
    var value = $(this).text();
    var input = $('#advanced-sqrt');
    input.val(input.val() + value);
    return false;
});

This somehow seem to unbind my enter key (on the keyboard) from submitting the form, and instead binding it to the first button, (.

I've struggled with this issue for quite some time now, and haven't been able to see how this peace of jQuery would have anything to do with keybinding?

After going to Google for help, I saw some talk about return false;, but removing it from my code does not seem to be an option (if I do so, my form submits every time I click a button)

Edit: To avoid confusion, I wish for 'Enter' to submit the form.

Albert MN.
  • 713
  • 1
  • 16
  • 33

2 Answers2

1

The JavaScript has very little to do with this.

When you submit a form by pressing enter in a text field, it triggers the first submit button in the form.

The button is getting triggered because it is first, not because you have attached JavaScript to it.

The JavaScript is returning false, and that is preventing the form from being submitted.

The quick solution is to not use submit buttons for the buttons you only want to trigger JS:

<button type="button">

(and to not return false from the = button which I assume you want to trigger form submission)

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

Not quite sure what you're asking...I will edit my answer if you reply, but if the problem is submitting the 'form'

why don't you just do something like :

$('form').submit();

after specifying the appropriate event.

Turk
  • 232
  • 3
  • 16
  • Edit: To avoid confusion, I wish for 'Enter' to submit the form. – Albert MN. Feb 10 '16 at 15:26
  • then I would suggest you use the ID for the enter button to define an event and then finish off with the line that I put in my answer. – Turk Feb 10 '16 at 15:28