views:

77

answers:

3

I looked into previous questions, but they didn't seem to answer what's happening to me.
In my real code i'm creating a form on the fly and adding to it two buttons, one for submission and another for other function. For this I'm setting the "type" attribute of the buttons to "submit" for one and "button" for the other. The problem is that in Chrome both buttons submit the form.

Code for the form:

form = $(document.createElement('form')).attr('method', 'get').attr('action', defaults.action).appendTo(object);

Code for the 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 more simple test with this HTML code:

<form action="" method="get"><button id="test">test</button></form>

When Chrome doesn't finds a submit button, any button submits the form.

The following doesn't works, the form gets submitted on button click:

$('#test').attr('type', 'button');

The following does works, the form does not submit on button click:

document.getElementById('test').setAttribute('type', 'button');

The form and the button are being generated dynamically and I'm using jQuery so attr() is the most obvious method. Is something wrong with the jQuery core and Chrome's JS specification? It works fine in Firefox. Thanks a lot.

A: 

I'm not 100% sure what you're asking, but I think you're asking about preventing the submission of a form with a button click in Chrome. If that is the case, why not use preventDefault?

$("#test").click(function(e) {
  e.preventDefault();
  //more work here....
});

EDIT:
I agree with Nick that in order to do what you are trying to do, go with the straight JavaScript approach. But in that case, you're applying attributes to a button that don't make sense (or at least aren't valid). Since you're already using jQuery, why not handle it properly and prevent the default action of a button click in a form?

David Hoerster
@D Hoerster: Thanks, but I'm not trying to prevent the form submission, I'm creating a form on the fly and appending to it two buttons, one for submit and another for other functionality. Everything works ok in Firefox, but in Chrome, both buttons submit, the one with the "type" attribute set to "submit" and the other one with the "type" attribute set to "button".
Alejandro García Iglesias
A: 

jQuery is working fine for me in Chrome ... all the functions I've thrown at it today are running just fine, including .attr()...

drachenstern
+3  A: 

First, the correct approach:
To do what you want in this case, go with the vanilla JavaScript solution, but test it in IE!


The why:
The reason type doesn't work is because it fails in IE (you can't chagne the type of an input after it's added to the DOM, and it's handled in this same way), so jQuery throws an error when you try. It does this specifically for <input> and <button> elements when changing the type attribute.

If you look in your console you'll see this:

Error: Uncaught type property can't be changed

Here's a quick test showing this, check the console to see the error jQuery throws.

Nick Craver
I knew that but I was a little mystified at the claim that it works in Firefox.
Pointy
@Pointy - I get the same error in Firefox here, perhaps he's using an older version of jquery with different logic?
Nick Craver
Probably something like that.
Pointy
Interesting - I never tried changing the type on an `input` before. I guess I know not to try it going forward. :) Thanks - nice insight.
David Hoerster
Thanks to all for the answers. I'm under jQuery 1.4.2. The main reason is that in my real code I'm creating an element and chaining all the methods for creation; then appending to DOM. In Firefox works ok, in Chrome not. The confusing part is that the test link you gave me doesn't works in FF either (?).
Alejandro García Iglesias
@profoundjoy - Correct, I'm not sure how this would work in Firefox at all...your best bet it to just create the element using `<button type="button">` initially :)
Nick Craver
@profoundjoy - To your update, you can swap this: `$(document.createElement('button')).attr('type', 'submit')` for this: `$('<button type="button" />')` and it should work, or create it all as a single string. Also, you can pass `.attr()` and object, for example: `$('<button type="button" />').attr({ 'class': classesHash.addButton, 'title': i18n('Add filter'), 'value': i18n('Add filter') }).append(i18n('Add filter')).click(addFilter);`
Nick Craver
Thanks to everyone.@Nick Craver: I'll go for the single-string solution. Thanks a lot!
Alejandro García Iglesias