find which object called function
If I have multiple objects with different IDs but they all call the same function when clicked. Is there any way to track or find which object called the function.
e.g
function test(){
/*function changes background color of 'div' and displays the id of the object that called the function*/
document.getElementById('div').style.backgroundColor = 'red';
var objectID = /*WHATEVER OBJECT CALLED FUNCTION*/;
document.getElementById('textbox').innerHTML = objectID;
}
cheers
javascript html
add a comment |
If I have multiple objects with different IDs but they all call the same function when clicked. Is there any way to track or find which object called the function.
e.g
function test(){
/*function changes background color of 'div' and displays the id of the object that called the function*/
document.getElementById('div').style.backgroundColor = 'red';
var objectID = /*WHATEVER OBJECT CALLED FUNCTION*/;
document.getElementById('textbox').innerHTML = objectID;
}
cheers
javascript html
2
did you know you can pass arguments to a function?function test(id)
then when you call the function liketest('someid')
thenid
will be'someid'
– Bravo
Nov 16 '18 at 0:24
1
Thank you, that will work! That's annoyingly simple haha.
– Elongated Muskrat
Nov 16 '18 at 0:26
of course, depending on how test is called, it could be simpler, but you haven't shown that very important piece of the puzzle :p
– Bravo
Nov 16 '18 at 0:27
add a comment |
If I have multiple objects with different IDs but they all call the same function when clicked. Is there any way to track or find which object called the function.
e.g
function test(){
/*function changes background color of 'div' and displays the id of the object that called the function*/
document.getElementById('div').style.backgroundColor = 'red';
var objectID = /*WHATEVER OBJECT CALLED FUNCTION*/;
document.getElementById('textbox').innerHTML = objectID;
}
cheers
javascript html
If I have multiple objects with different IDs but they all call the same function when clicked. Is there any way to track or find which object called the function.
e.g
function test(){
/*function changes background color of 'div' and displays the id of the object that called the function*/
document.getElementById('div').style.backgroundColor = 'red';
var objectID = /*WHATEVER OBJECT CALLED FUNCTION*/;
document.getElementById('textbox').innerHTML = objectID;
}
cheers
javascript html
javascript html
asked Nov 16 '18 at 0:22
Elongated MuskratElongated Muskrat
93
93
2
did you know you can pass arguments to a function?function test(id)
then when you call the function liketest('someid')
thenid
will be'someid'
– Bravo
Nov 16 '18 at 0:24
1
Thank you, that will work! That's annoyingly simple haha.
– Elongated Muskrat
Nov 16 '18 at 0:26
of course, depending on how test is called, it could be simpler, but you haven't shown that very important piece of the puzzle :p
– Bravo
Nov 16 '18 at 0:27
add a comment |
2
did you know you can pass arguments to a function?function test(id)
then when you call the function liketest('someid')
thenid
will be'someid'
– Bravo
Nov 16 '18 at 0:24
1
Thank you, that will work! That's annoyingly simple haha.
– Elongated Muskrat
Nov 16 '18 at 0:26
of course, depending on how test is called, it could be simpler, but you haven't shown that very important piece of the puzzle :p
– Bravo
Nov 16 '18 at 0:27
2
2
did you know you can pass arguments to a function?
function test(id)
then when you call the function like test('someid')
then id
will be 'someid'
– Bravo
Nov 16 '18 at 0:24
did you know you can pass arguments to a function?
function test(id)
then when you call the function like test('someid')
then id
will be 'someid'
– Bravo
Nov 16 '18 at 0:24
1
1
Thank you, that will work! That's annoyingly simple haha.
– Elongated Muskrat
Nov 16 '18 at 0:26
Thank you, that will work! That's annoyingly simple haha.
– Elongated Muskrat
Nov 16 '18 at 0:26
of course, depending on how test is called, it could be simpler, but you haven't shown that very important piece of the puzzle :p
– Bravo
Nov 16 '18 at 0:27
of course, depending on how test is called, it could be simpler, but you haven't shown that very important piece of the puzzle :p
– Bravo
Nov 16 '18 at 0:27
add a comment |
2 Answers
2
active
oldest
votes
If you're using
onclick="test()"
in the element, change it so that it passes itself to the function:
onclick="test(this)"
Then the function receives the object as the argument.
function test(element) {
var objectID = element.id;
...
}
add a comment |
Just do this:
function test(id) {
document.getElementById('div').style.backgroundColor = 'red';
var objectID = id;
document.getElementById('textbox').innerHTML = objectID;
}
var object = {
id = "AnObject",
testFunction: function() {
test(this.id);
}
};
object.testFunction();
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%2f53329700%2ffind-which-object-called-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you're using
onclick="test()"
in the element, change it so that it passes itself to the function:
onclick="test(this)"
Then the function receives the object as the argument.
function test(element) {
var objectID = element.id;
...
}
add a comment |
If you're using
onclick="test()"
in the element, change it so that it passes itself to the function:
onclick="test(this)"
Then the function receives the object as the argument.
function test(element) {
var objectID = element.id;
...
}
add a comment |
If you're using
onclick="test()"
in the element, change it so that it passes itself to the function:
onclick="test(this)"
Then the function receives the object as the argument.
function test(element) {
var objectID = element.id;
...
}
If you're using
onclick="test()"
in the element, change it so that it passes itself to the function:
onclick="test(this)"
Then the function receives the object as the argument.
function test(element) {
var objectID = element.id;
...
}
answered Nov 16 '18 at 1:00
BarmarBarmar
434k36258359
434k36258359
add a comment |
add a comment |
Just do this:
function test(id) {
document.getElementById('div').style.backgroundColor = 'red';
var objectID = id;
document.getElementById('textbox').innerHTML = objectID;
}
var object = {
id = "AnObject",
testFunction: function() {
test(this.id);
}
};
object.testFunction();
add a comment |
Just do this:
function test(id) {
document.getElementById('div').style.backgroundColor = 'red';
var objectID = id;
document.getElementById('textbox').innerHTML = objectID;
}
var object = {
id = "AnObject",
testFunction: function() {
test(this.id);
}
};
object.testFunction();
add a comment |
Just do this:
function test(id) {
document.getElementById('div').style.backgroundColor = 'red';
var objectID = id;
document.getElementById('textbox').innerHTML = objectID;
}
var object = {
id = "AnObject",
testFunction: function() {
test(this.id);
}
};
object.testFunction();
Just do this:
function test(id) {
document.getElementById('div').style.backgroundColor = 'red';
var objectID = id;
document.getElementById('textbox').innerHTML = objectID;
}
var object = {
id = "AnObject",
testFunction: function() {
test(this.id);
}
};
object.testFunction();
answered Nov 16 '18 at 0:47
Jack BashfordJack Bashford
13.2k31847
13.2k31847
add a comment |
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.
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%2f53329700%2ffind-which-object-called-function%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
2
did you know you can pass arguments to a function?
function test(id)
then when you call the function liketest('someid')
thenid
will be'someid'
– Bravo
Nov 16 '18 at 0:24
1
Thank you, that will work! That's annoyingly simple haha.
– Elongated Muskrat
Nov 16 '18 at 0:26
of course, depending on how test is called, it could be simpler, but you haven't shown that very important piece of the puzzle :p
– Bravo
Nov 16 '18 at 0:27