0

I've got some problem with my jQuery code. I've got this code:

$(document).ready({
  $('#es').click(function(){
    console.log("hola");
  });
})

And this output:

Uncaught SyntaxError: Unexpected string Blockquote

In this line:

$('#es').click(function()

I read other posts but they couldn't help me. Any solution?

1
  • 1
    The method ready expects a method as the first param, you're passing an object. This will never work. Have you read the jQuery documentation for ready? It has some good examples. Commented May 22, 2019 at 13:36

1 Answer 1

1

Check the documentation (and any other examples you may have seen) more carefully. .ready() expects a function as the first argument, but you are providing an object instead. The documentation says:

The .ready() method is typically used with an anonymous function:

$( document ).ready(function() {

// Handler for .ready() called.

});

Using your code it would be like this:

$(document).ready(function() {
  $('#es').click(function(){
    console.log("hola");
  });
});

Or you could use the shorthand version:

$(function() {
  $('#es').click(function(){
    console.log("hola");
  });
});

N.B.It needs to be given a function, so that it can save that function for later, and then execute it when the page is ready. This type of saved-for-later function reference is often referred to as a "callback" function.

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

1 Comment

ok great. They should both work, btw, but yes the second one is quicker to write :-)

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.