Pirzada
Pirzada

Reputation: 4713

what is difference between .submit() vs :submit?

Someone tell me the difference between .submit() vs :submit. When & where to use with a simple example please.

Thanks

Upvotes: 0

Views: 526

Answers (5)

user669677
user669677

Reputation:

$("html_element").X().Y().Z() - function: do something with the selected html_element

eaxample - in this case do something= modify the css property:

$("li").css({"color":"orange"});


$("html_element:X") - selector: filter somehow the selected html_element

eaxample - in this case all "li" elements was reduced to only the first one

$("li:nth-child(1)").css({"color":"red"});

Upvotes: 1

Ry-
Ry-

Reputation: 224912

The :submit pseudo-class will match <input> elements with a type of submit. The .submit() method is completely different; depending on what you pass it, it will either submit a form or add an event listener to one.

Here's a quick example:

$(':submit'); // will return all <input type="submit">
$('form:submit'); // shouldn't ever return anything
$('form').submit(); // submits all forms
$('form').submit(function(e) {
    e.preventDefault();
}); // disallows any forms to be submitted

For more information, see the jQuery API documentation on .submit().

Upvotes: 1

JMax
JMax

Reputation: 26591

.submit() is a method. That means you need to call it when you want to trigger a form submission

$("#form").submit()

:submit is a selector helper to find specifically buttons that submits

$("button:submit")

You can find very valuable examples on the jquery doc pages linked in my answer.

Upvotes: 3

Bozho
Bozho

Reputation: 597106

  • .submit() is a function that submits a form.
  • :submit is a selector to find submit buttons

Upvotes: 3

Anthony Grist
Anthony Grist

Reputation: 38345

.submit() is a function, used to submit a form. :submit is a selector, used to identify <input type="submit"> elements.

Example:

<form id="myForm">
    <input type="submit" id="mySubmitButton" value="Click me!">
</form>

$('#myForm').submit(); // submits the form
$(':submit'); // selects the submit button

Upvotes: 11

Related Questions