- This topic has 3 replies, 3 voices, and was last updated 4 years ago by .
Viewing 3 posts - 1 through 3 (of 3 total)
Viewing 3 posts - 1 through 3 (of 3 total)
- The forum ‘CSS’ is closed to new topics and replies.
The forums ran from 2008-2020 and are now closed and viewable here as an archive.
Hello,
What is the proper way to pass an argument to a function in jQuery?
I want to pass a color to a function to update multiple css attributes
I would think it would be something like below, but I can’t seem to get it to work.
thanks
iosman
I can’t see why this would be any different from vanilla javascript. My best advice would be to start reading here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters and further.
There is an exception for example when you do something like this: button.addEventListener('click', myFunction);
, in that case the argument passed is the event.
It’s jQuery equivalent would be something like this: $('button').on('click', myFunction);
Passing an argument to that function would then be something like this: $('button').on('click', myFunction('test'));
.
And if you still want to have the event in your array of arguments you can do something like this: $('button').on('click', function (e) { myFunction(e, 'test') });
I’m noticing a trend in these forums where a relevant question is posted, then after a reply it’s re-posted with links in, with the text “thanks my issue has been fixed”. I think these are fake questions with the sole goal to get people to click those links.