I reviewed the previous questions, but they did not seem to answer what was happening to me.
In my real code, I create an on-the-fly form and add two buttons to it, one for submitting and another for another function. To do this, I set the "type" attribute for the "submit" buttons for one and "button" for the other. The problem is that in Chrome, both buttons represent a form.
Code for form:
form = $(document.createElement('form')).attr('method', 'get').attr('action', defaults.action).appendTo(object);
Code for buttons:
form.append( $(document.createElement('div')). attr('class', classesHash.buttonsContainer). append( $(document.createElement('button')). attr('type', 'submit'). addClass(classesHash.submitButton). attr('title', i18n('Filter')). attr('value', i18n('Filter')). append(i18n('Filter')) ). append( $(document.createElement('button')). attr('type', 'button'). addClass(classesHash.addButton). attr('title', i18n('Add filter')). attr('value', i18n('Add filter')). append(i18n('Add filter')). click(addFilter) ) );
I made a simpler test with this HTML code:
<form action="" method="get"><button id="test">test</button></form>
When Chrome does not find the submit button, any button submits a form.
The following actions are not performed, the form is submitted when the button is clicked:
$('#test').attr('type', 'button');
The following is performed: the form is not submitted when the button is clicked:
document.getElementById('test').setAttribute('type', 'button');
The form and button are generated dynamically, and I use jQuery, so attr () is the most obvious method. Is there something wrong with the jQuery core and the Chrome JS spec? It works fine in Firefox. Many thanks.