3

I am newbie to jQuery, can someone explain what this code does:

$("#currency form").submit(function(e) {

        triggers.eq(1).overlay().close();
        return e.preventDefault();
    });

3 Answers 3

12

The first line begins a function that handles the submit event of all form tag(s) in the element with ID currency.
Documentation: Selectors, submit event

The second line closes an overlay in the second element in the triggers variable.
Documentation: eq method, overlay plugin

The third line tries to prevent the submit, but isn't completely correct. (It should be e.preventDefault(); and/or return false;)
Documentation: event.preventDefault, event handlers

Sign up to request clarification or add additional context in comments.

3 Comments

+1 for correctness, without the "return false;" it might get submitted anyway
@Alex: Wrong. e.preventDefault() will prevent the submission, even without return false.
Fair enough, I still always add return false anyway! :P
2

triggers = a jQuery object

triggers.eq(1) = get the second element from the matched elements inside the jquery object

triggers.eq(1).overlay() = get the overlay instance (a plugin) on the second element

triggers.eq(1).overlay().close() = close the overlay.

return e.preventDefault(); = prevent the default action (form submission)

Comments

2

On the submit event of the form, it will:

  1. Get the second element in the triggers collection (of jQuery elements).
  2. Get the overlay on that element.
  3. Close that overlay.
  4. Prevent the submit event from bubbling to the parent handler.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.