2

I have the following code:

$(document).ready(function(){    
    $("button.continue").click(function(){
        $(this).html("Hello!");
    });
});

Which works perfect but I want to turn the whole thing into something like this:

$(document).ready(function(){    
    $("button.continue").click().html("Hello!");
});

My questions are:

  1. Can I use click() and a following event, in this case html() in that way (Without turning to the function part)?
  2. If not, why can I not do this since I would think of the workings behind this, something like: When user clicks on the element selected, change the HTML text in it to something else.
  3. If this is not the way to think or reason the logic behind this, how should I approach this
2
  • 6
    This isn't how javascript works. It can't read your mind, and it requires following the spec to have it behave as you intend. Same is true for all languages by the way... Commented Feb 18, 2013 at 23:24
  • 3
    Fundamentally: no, you can't do this. .click() with no arguments is a shorthand for .trigger('click'). Commented Feb 18, 2013 at 23:37

2 Answers 2

5

A jQuery function returns the object itself, so you can chain methods like:

element.css('attribute', 'value').html('value').append('element').click(function() {});

But when binding an event handler, which is what .click() does, it has to store the logic in a callback that is called when the event happens.

An example of how the chaining actually works:

var testObject = {
    methodA : function() {
        // some logic
        return this;
    },
    methodB : function() {
        // some logic
        return this;
    }
}
// you can than simply call
testObject.methodA().methodB();
Sign up to request clarification or add additional context in comments.

2 Comments

Actually... invoking click() without any parameters simulates a click event IIRC.
You're right - missed that last one in the hurry. It's now updated. Thanks for pointing that out.
0

One could construct an API that works as you desire, but this is not, in fact, the way that jQuery works, which is the specific library you're using. The jQuery click method takes an event listener function which is called whenever a matched element is clicked. This is a typical functional programmer pattern which is commonly used in modern, event-based UI.

If you think this style of code is a bit unreadable, try CoffeeScript, a language that compiles down to javascript and makes common patterns like this more convenient to implement. The equivalent in CoffeeScript would be something like:

$(document).ready ->
    $('button.continue').click ->
        $(this).html "Hello"

For more information, check out: http://coffeescript.org/

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.