Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

How to pass an argument without the function firing

    var createGroup = function(groupName){
    var name = groupName;
    }

    newGroup.on("click", createGroup(nameOfGroup));

How do I make it so the function named 'createGroup' can receive an argument from a click event without being fired on load rather than when the button is clicked? I need to make the same function work in two places, which is what I need the arguments for, to select the text field for the corresponding button.

1 Answer

Try something like so:

$newGroup.on('click', function(event) {
     event.preventDefault();
     createGroup(nameOfGroup);
});

The createGroup function will only be executed on the click event.

I'm also assuming that "newGroup" is a jQuery object (it wasn't defined in your code) so I changed the variable name to begin with a $ symbol as it's a best practice.

Cheers!