DeeRain
DeeRain

Reputation: 1354

addEventListener submit and jQuery submit

I have form:

<form id="form1" class="f_class">
<input type="button" class="jq-button" value="jq-button"/>
</form>

And js code:

var f=document.getElementById("form1");
f.addEventListener("submit", function(){alert("addEventListener_submit");},false);
$(".jq-button").click(function(){$("#form1").submit();});

(Jsbin)

When I click to jq-button alert does not show. Why?

UPDATE

I have external js code, which use addEventListener, and I can't change it. But in my code I want use jQuery submit. Can I combined them?

Upvotes: 0

Views: 9449

Answers (2)

voigtan
voigtan

Reputation: 9031

$().submit() triggers the jquery submit event and not the eventlisterner. I would have written it with jQuery all of the binding of events like:

var f = $("#form1");
f.on("submit", function() { // on requires jquery 1.7+
    alert("submit");
});
$(".jq-button").on("click", (function(e) {
    $(this).closest("form").trigger("submit");
    e.preventDefault();
});

Edit:

you could do something like:

var f=document.getElementById("form1");

f.addEventListener("submit", function(){
    alert("addEventListener_submit");
},false);

$(".jq-button").click(function(){
    var c = $(this),
        cc = c.clone().attr("type","submit");
    c.replaceWith(cc);
    cc.click();
});

if you can´t change the HTML markup.

Upvotes: 0

Ch4rAss
Ch4rAss

Reputation: 754

just correct it to:

<input type="submit" class="jq-button" value="jq-button"/>

Upvotes: 4

Related Questions