0
$(document).ready(function(){
    $("button").click(func(){
        newfunc();
    });
});

I've seen this is caused by un-escaped quotes...but I'm just trying to replicate a tutorial from the web. Not sure why I'm getting this error on such a simple piece of code.

3
  • change func to function in line 2 $("button").click(func(){ => $("button").click(function(){ Commented Nov 14, 2015 at 9:38
  • Write $("button").click(function(){ instead of $("button").click(func Commented Nov 14, 2015 at 9:39
  • Shouldn't it be click(function (){}) Commented Nov 14, 2015 at 9:39

3 Answers 3

1

You should change func to function. In the click method you have to pass a callback function. If you have declared earlier a function, pass there a reference to this function. Otherwiser pass an anonymous function like below and define inside it's body, what you want to be executed when the button is clicked.

$(document).ready(function(){
    $("button").click(function(){
        newfunc();
    });
});

By the way propably you have also to change your selector, $("button"). If the button has a specific id, for instance buttonId, then use this: $("#buttonId"). While if the button has a specific class, for instance js-button, then use this: $(".js-button")

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

Comments

0
$(document).ready(function(){
    $("button").click(function(){
        newfunc();
    });
});

func should be function, it is a function() execute on click.

Comments

0

You just got simple syntax error; If you ment to use func, you don't declare it's body, here's cases

Consider this

var func = function(){};

//RIGHT
$("button").click(func); //callback function

//RIGHT
$("button").click(function func(){}); //we just creating new named function, named callback

//RIGHT
$("button").click(function(){}); //anonymous callback function

//WRONG
$("button").click(func(){}); //anonymous callback function

Although answer is simple, here's also demo

$(document).ready(function(){
    function newfunc(elem){
        $(elem).css('background', 'red');
    }
    $("button").click(function(){
        newfunc(this);
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>click me</button>

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.