jQuery Pass Custom Parameter to Common .click() Handler
I have a Click-handler defined which handles clicks on an LI menu item. Suppose the code highlights the active item.
$('.menu li').click(function () {
$(this).siblings().removeClass('active');
$(this).addClass('active');
// etc.
}
On startup, I also simulate a click by calling this handler manually. This is required to highlight an item coming from a server-side URL, which will populate hidden fields. The LI Items have ID's so I manually invoke .click()
on them.
$('document').ready(function () {
if($("#queueFilter").val() == "MENU1") {
$("#menu1").click();
} else if($("#queueFilter").val() == "MENU2") {
$("#menu2").click();
} else {
$("#menu3").click();
}
}
Problem: I need to distinguish the simulated startup click()
invocation from the real user-initiated one. I thought of passing a custom param somehow, but it's not working. Is it possible to specify a custom handler param, which will be NULL in the other case?
Ideally what I want is, on startup:
$("#menu1").click(false); // Indicates a simulated click
Then in the handler definition:
$('.menu li').click(customarg, function () { .. }));
A custom arg would allow me to distinguish in my handler code where I'm coming from and proceed accordingly.
jquery
add a comment |
I have a Click-handler defined which handles clicks on an LI menu item. Suppose the code highlights the active item.
$('.menu li').click(function () {
$(this).siblings().removeClass('active');
$(this).addClass('active');
// etc.
}
On startup, I also simulate a click by calling this handler manually. This is required to highlight an item coming from a server-side URL, which will populate hidden fields. The LI Items have ID's so I manually invoke .click()
on them.
$('document').ready(function () {
if($("#queueFilter").val() == "MENU1") {
$("#menu1").click();
} else if($("#queueFilter").val() == "MENU2") {
$("#menu2").click();
} else {
$("#menu3").click();
}
}
Problem: I need to distinguish the simulated startup click()
invocation from the real user-initiated one. I thought of passing a custom param somehow, but it's not working. Is it possible to specify a custom handler param, which will be NULL in the other case?
Ideally what I want is, on startup:
$("#menu1").click(false); // Indicates a simulated click
Then in the handler definition:
$('.menu li').click(customarg, function () { .. }));
A custom arg would allow me to distinguish in my handler code where I'm coming from and proceed accordingly.
jquery
add a comment |
I have a Click-handler defined which handles clicks on an LI menu item. Suppose the code highlights the active item.
$('.menu li').click(function () {
$(this).siblings().removeClass('active');
$(this).addClass('active');
// etc.
}
On startup, I also simulate a click by calling this handler manually. This is required to highlight an item coming from a server-side URL, which will populate hidden fields. The LI Items have ID's so I manually invoke .click()
on them.
$('document').ready(function () {
if($("#queueFilter").val() == "MENU1") {
$("#menu1").click();
} else if($("#queueFilter").val() == "MENU2") {
$("#menu2").click();
} else {
$("#menu3").click();
}
}
Problem: I need to distinguish the simulated startup click()
invocation from the real user-initiated one. I thought of passing a custom param somehow, but it's not working. Is it possible to specify a custom handler param, which will be NULL in the other case?
Ideally what I want is, on startup:
$("#menu1").click(false); // Indicates a simulated click
Then in the handler definition:
$('.menu li').click(customarg, function () { .. }));
A custom arg would allow me to distinguish in my handler code where I'm coming from and proceed accordingly.
jquery
I have a Click-handler defined which handles clicks on an LI menu item. Suppose the code highlights the active item.
$('.menu li').click(function () {
$(this).siblings().removeClass('active');
$(this).addClass('active');
// etc.
}
On startup, I also simulate a click by calling this handler manually. This is required to highlight an item coming from a server-side URL, which will populate hidden fields. The LI Items have ID's so I manually invoke .click()
on them.
$('document').ready(function () {
if($("#queueFilter").val() == "MENU1") {
$("#menu1").click();
} else if($("#queueFilter").val() == "MENU2") {
$("#menu2").click();
} else {
$("#menu3").click();
}
}
Problem: I need to distinguish the simulated startup click()
invocation from the real user-initiated one. I thought of passing a custom param somehow, but it's not working. Is it possible to specify a custom handler param, which will be NULL in the other case?
Ideally what I want is, on startup:
$("#menu1").click(false); // Indicates a simulated click
Then in the handler definition:
$('.menu li').click(customarg, function () { .. }));
A custom arg would allow me to distinguish in my handler code where I'm coming from and proceed accordingly.
jquery
jquery
asked Dec 27 '18 at 18:12
gene b.
1,64852554
1,64852554
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
You can use the .trigger
method from jQuery. It allows to send some parameters to the event handler.
Take a look here: http://api.jquery.com/trigger/
It would be like this : $('.menu li').trigger('click', { key: value })
add a comment |
Which version of jQuery are you using?
According to the jQuery API documentation you can pass any data you wish to .click() starting from 1.4.3.
The implementation would look something like this:
$('document').ready(function () {
if($("#queueFilter").val() == "MENU1") {
$("#menu1").click({isSimulated: true}, handleMenuClick);
} else if($("#queueFilter").val() == "MENU2") {
$("#menu2").click({isSimulated: true}, handleMenuClick);
} else {
$("#menu3").click({isSimulated: true}, handleMenuClick);
}
}
function handleMenuClick(event){
let clickedItem = $(event.target);
console.log(clickedItem);
if (!!event.data && event.data.isSimulated === true) {
console.log("Simulated click");
} else {
console.log("Real click");
}
clickedItem.siblings().removeClass('active');
clickedItem.addClass('active');
};
New contributor
add a comment |
I just found the solution: use trigger()
and .on()
as follows,
Call the click as
$("#menu1").trigger('click', false);
and the handler definition is
$('.menu li').on('click', function (e, actualUserClick) {
...
});
In the case where the arg isn't passed (real user click) this arg is undefined
.
1
Wouldn't you want it to be "fakeUserClick" then? As bothfalse
andundefined
are falsy, :)
– Taplar
Dec 27 '18 at 18:27
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53949163%2fjquery-pass-custom-parameter-to-common-click-handler%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use the .trigger
method from jQuery. It allows to send some parameters to the event handler.
Take a look here: http://api.jquery.com/trigger/
It would be like this : $('.menu li').trigger('click', { key: value })
add a comment |
You can use the .trigger
method from jQuery. It allows to send some parameters to the event handler.
Take a look here: http://api.jquery.com/trigger/
It would be like this : $('.menu li').trigger('click', { key: value })
add a comment |
You can use the .trigger
method from jQuery. It allows to send some parameters to the event handler.
Take a look here: http://api.jquery.com/trigger/
It would be like this : $('.menu li').trigger('click', { key: value })
You can use the .trigger
method from jQuery. It allows to send some parameters to the event handler.
Take a look here: http://api.jquery.com/trigger/
It would be like this : $('.menu li').trigger('click', { key: value })
answered Dec 27 '18 at 18:22
Arthur Almeida
29915
29915
add a comment |
add a comment |
Which version of jQuery are you using?
According to the jQuery API documentation you can pass any data you wish to .click() starting from 1.4.3.
The implementation would look something like this:
$('document').ready(function () {
if($("#queueFilter").val() == "MENU1") {
$("#menu1").click({isSimulated: true}, handleMenuClick);
} else if($("#queueFilter").val() == "MENU2") {
$("#menu2").click({isSimulated: true}, handleMenuClick);
} else {
$("#menu3").click({isSimulated: true}, handleMenuClick);
}
}
function handleMenuClick(event){
let clickedItem = $(event.target);
console.log(clickedItem);
if (!!event.data && event.data.isSimulated === true) {
console.log("Simulated click");
} else {
console.log("Real click");
}
clickedItem.siblings().removeClass('active');
clickedItem.addClass('active');
};
New contributor
add a comment |
Which version of jQuery are you using?
According to the jQuery API documentation you can pass any data you wish to .click() starting from 1.4.3.
The implementation would look something like this:
$('document').ready(function () {
if($("#queueFilter").val() == "MENU1") {
$("#menu1").click({isSimulated: true}, handleMenuClick);
} else if($("#queueFilter").val() == "MENU2") {
$("#menu2").click({isSimulated: true}, handleMenuClick);
} else {
$("#menu3").click({isSimulated: true}, handleMenuClick);
}
}
function handleMenuClick(event){
let clickedItem = $(event.target);
console.log(clickedItem);
if (!!event.data && event.data.isSimulated === true) {
console.log("Simulated click");
} else {
console.log("Real click");
}
clickedItem.siblings().removeClass('active');
clickedItem.addClass('active');
};
New contributor
add a comment |
Which version of jQuery are you using?
According to the jQuery API documentation you can pass any data you wish to .click() starting from 1.4.3.
The implementation would look something like this:
$('document').ready(function () {
if($("#queueFilter").val() == "MENU1") {
$("#menu1").click({isSimulated: true}, handleMenuClick);
} else if($("#queueFilter").val() == "MENU2") {
$("#menu2").click({isSimulated: true}, handleMenuClick);
} else {
$("#menu3").click({isSimulated: true}, handleMenuClick);
}
}
function handleMenuClick(event){
let clickedItem = $(event.target);
console.log(clickedItem);
if (!!event.data && event.data.isSimulated === true) {
console.log("Simulated click");
} else {
console.log("Real click");
}
clickedItem.siblings().removeClass('active');
clickedItem.addClass('active');
};
New contributor
Which version of jQuery are you using?
According to the jQuery API documentation you can pass any data you wish to .click() starting from 1.4.3.
The implementation would look something like this:
$('document').ready(function () {
if($("#queueFilter").val() == "MENU1") {
$("#menu1").click({isSimulated: true}, handleMenuClick);
} else if($("#queueFilter").val() == "MENU2") {
$("#menu2").click({isSimulated: true}, handleMenuClick);
} else {
$("#menu3").click({isSimulated: true}, handleMenuClick);
}
}
function handleMenuClick(event){
let clickedItem = $(event.target);
console.log(clickedItem);
if (!!event.data && event.data.isSimulated === true) {
console.log("Simulated click");
} else {
console.log("Real click");
}
clickedItem.siblings().removeClass('active');
clickedItem.addClass('active');
};
New contributor
New contributor
answered Dec 27 '18 at 18:25
Laurens Deprost
87114
87114
New contributor
New contributor
add a comment |
add a comment |
I just found the solution: use trigger()
and .on()
as follows,
Call the click as
$("#menu1").trigger('click', false);
and the handler definition is
$('.menu li').on('click', function (e, actualUserClick) {
...
});
In the case where the arg isn't passed (real user click) this arg is undefined
.
1
Wouldn't you want it to be "fakeUserClick" then? As bothfalse
andundefined
are falsy, :)
– Taplar
Dec 27 '18 at 18:27
add a comment |
I just found the solution: use trigger()
and .on()
as follows,
Call the click as
$("#menu1").trigger('click', false);
and the handler definition is
$('.menu li').on('click', function (e, actualUserClick) {
...
});
In the case where the arg isn't passed (real user click) this arg is undefined
.
1
Wouldn't you want it to be "fakeUserClick" then? As bothfalse
andundefined
are falsy, :)
– Taplar
Dec 27 '18 at 18:27
add a comment |
I just found the solution: use trigger()
and .on()
as follows,
Call the click as
$("#menu1").trigger('click', false);
and the handler definition is
$('.menu li').on('click', function (e, actualUserClick) {
...
});
In the case where the arg isn't passed (real user click) this arg is undefined
.
I just found the solution: use trigger()
and .on()
as follows,
Call the click as
$("#menu1").trigger('click', false);
and the handler definition is
$('.menu li').on('click', function (e, actualUserClick) {
...
});
In the case where the arg isn't passed (real user click) this arg is undefined
.
answered Dec 27 '18 at 18:23
gene b.
1,64852554
1,64852554
1
Wouldn't you want it to be "fakeUserClick" then? As bothfalse
andundefined
are falsy, :)
– Taplar
Dec 27 '18 at 18:27
add a comment |
1
Wouldn't you want it to be "fakeUserClick" then? As bothfalse
andundefined
are falsy, :)
– Taplar
Dec 27 '18 at 18:27
1
1
Wouldn't you want it to be "fakeUserClick" then? As both
false
and undefined
are falsy, :)– Taplar
Dec 27 '18 at 18:27
Wouldn't you want it to be "fakeUserClick" then? As both
false
and undefined
are falsy, :)– Taplar
Dec 27 '18 at 18:27
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53949163%2fjquery-pass-custom-parameter-to-common-click-handler%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown