What all information we can pass as parameters to inline event handlers?
I found this code on internet where author is passing id of the element as first parameter to the event handler. Refer to the sample below.
Until today, I was under the impression that we can pass only event information as argument to the event handler and I can see that we can pass id of current element also as parameter to event handler.
Where is this documented on MDN? Can someone point to me the documentation?
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<input type="text" value="Hello" onchange="someFunction(id,event);" id="someId"/>
</body>
<script type="text/javascript">
function someFunction(id,event){
console.log(id); //Printing "someId"
console.log(event.target.value); //It is printing updated value
}
</script>
</html>
javascript html
add a comment |
I found this code on internet where author is passing id of the element as first parameter to the event handler. Refer to the sample below.
Until today, I was under the impression that we can pass only event information as argument to the event handler and I can see that we can pass id of current element also as parameter to event handler.
Where is this documented on MDN? Can someone point to me the documentation?
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<input type="text" value="Hello" onchange="someFunction(id,event);" id="someId"/>
</body>
<script type="text/javascript">
function someFunction(id,event){
console.log(id); //Printing "someId"
console.log(event.target.value); //It is printing updated value
}
</script>
</html>
javascript html
Yes, it is possible to get id asevent.target.idbut I am curious as where is it documented that we can passIdof element also?
– javanoob
Jan 1 at 1:19
developer.mozilla.org/en-US/docs/Web/API/Element
– ecg8
Jan 1 at 1:24
add a comment |
I found this code on internet where author is passing id of the element as first parameter to the event handler. Refer to the sample below.
Until today, I was under the impression that we can pass only event information as argument to the event handler and I can see that we can pass id of current element also as parameter to event handler.
Where is this documented on MDN? Can someone point to me the documentation?
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<input type="text" value="Hello" onchange="someFunction(id,event);" id="someId"/>
</body>
<script type="text/javascript">
function someFunction(id,event){
console.log(id); //Printing "someId"
console.log(event.target.value); //It is printing updated value
}
</script>
</html>
javascript html
I found this code on internet where author is passing id of the element as first parameter to the event handler. Refer to the sample below.
Until today, I was under the impression that we can pass only event information as argument to the event handler and I can see that we can pass id of current element also as parameter to event handler.
Where is this documented on MDN? Can someone point to me the documentation?
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<input type="text" value="Hello" onchange="someFunction(id,event);" id="someId"/>
</body>
<script type="text/javascript">
function someFunction(id,event){
console.log(id); //Printing "someId"
console.log(event.target.value); //It is printing updated value
}
</script>
</html>
javascript html
javascript html
edited Jan 1 at 1:45
javanoob
asked Jan 1 at 1:14
javanoobjavanoob
2,065134675
2,065134675
Yes, it is possible to get id asevent.target.idbut I am curious as where is it documented that we can passIdof element also?
– javanoob
Jan 1 at 1:19
developer.mozilla.org/en-US/docs/Web/API/Element
– ecg8
Jan 1 at 1:24
add a comment |
Yes, it is possible to get id asevent.target.idbut I am curious as where is it documented that we can passIdof element also?
– javanoob
Jan 1 at 1:19
developer.mozilla.org/en-US/docs/Web/API/Element
– ecg8
Jan 1 at 1:24
Yes, it is possible to get id as
event.target.id but I am curious as where is it documented that we can pass Id of element also?– javanoob
Jan 1 at 1:19
Yes, it is possible to get id as
event.target.id but I am curious as where is it documented that we can pass Id of element also?– javanoob
Jan 1 at 1:19
developer.mozilla.org/en-US/docs/Web/API/Element
– ecg8
Jan 1 at 1:24
developer.mozilla.org/en-US/docs/Web/API/Element
– ecg8
Jan 1 at 1:24
add a comment |
1 Answer
1
active
oldest
votes
I believe you can refer to any property that's somewhere in the element's prototype chain as a standalone variable:
function someFunction(id, onclick, children, clientTop) {
console.log(id); // someId
console.log(onclick); // note, this is null, not undefined!
console.log(children); // length 0, but still an HTMLCollection
console.log(clientTop); // 2
}<input type="text" value="Hello" onchange="someFunction(id, onclick, children, clientTop);" id="someId" />It's as if the inline handler is wrapped in a with(this). Referencing a property name that exists on the element object, or in the element's prototype chain, will result in that property value being referenced.
So, an inline handler like
<input onchange="somestr">
is interpreted something like
// assume this refers to that input element:
with (this) {
eval(somestr);
}
Note that event is in a different category - it's not a property of the element, it's a global window.event.
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%2f53992456%2fwhat-all-information-we-can-pass-as-parameters-to-inline-event-handlers%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I believe you can refer to any property that's somewhere in the element's prototype chain as a standalone variable:
function someFunction(id, onclick, children, clientTop) {
console.log(id); // someId
console.log(onclick); // note, this is null, not undefined!
console.log(children); // length 0, but still an HTMLCollection
console.log(clientTop); // 2
}<input type="text" value="Hello" onchange="someFunction(id, onclick, children, clientTop);" id="someId" />It's as if the inline handler is wrapped in a with(this). Referencing a property name that exists on the element object, or in the element's prototype chain, will result in that property value being referenced.
So, an inline handler like
<input onchange="somestr">
is interpreted something like
// assume this refers to that input element:
with (this) {
eval(somestr);
}
Note that event is in a different category - it's not a property of the element, it's a global window.event.
add a comment |
I believe you can refer to any property that's somewhere in the element's prototype chain as a standalone variable:
function someFunction(id, onclick, children, clientTop) {
console.log(id); // someId
console.log(onclick); // note, this is null, not undefined!
console.log(children); // length 0, but still an HTMLCollection
console.log(clientTop); // 2
}<input type="text" value="Hello" onchange="someFunction(id, onclick, children, clientTop);" id="someId" />It's as if the inline handler is wrapped in a with(this). Referencing a property name that exists on the element object, or in the element's prototype chain, will result in that property value being referenced.
So, an inline handler like
<input onchange="somestr">
is interpreted something like
// assume this refers to that input element:
with (this) {
eval(somestr);
}
Note that event is in a different category - it's not a property of the element, it's a global window.event.
add a comment |
I believe you can refer to any property that's somewhere in the element's prototype chain as a standalone variable:
function someFunction(id, onclick, children, clientTop) {
console.log(id); // someId
console.log(onclick); // note, this is null, not undefined!
console.log(children); // length 0, but still an HTMLCollection
console.log(clientTop); // 2
}<input type="text" value="Hello" onchange="someFunction(id, onclick, children, clientTop);" id="someId" />It's as if the inline handler is wrapped in a with(this). Referencing a property name that exists on the element object, or in the element's prototype chain, will result in that property value being referenced.
So, an inline handler like
<input onchange="somestr">
is interpreted something like
// assume this refers to that input element:
with (this) {
eval(somestr);
}
Note that event is in a different category - it's not a property of the element, it's a global window.event.
I believe you can refer to any property that's somewhere in the element's prototype chain as a standalone variable:
function someFunction(id, onclick, children, clientTop) {
console.log(id); // someId
console.log(onclick); // note, this is null, not undefined!
console.log(children); // length 0, but still an HTMLCollection
console.log(clientTop); // 2
}<input type="text" value="Hello" onchange="someFunction(id, onclick, children, clientTop);" id="someId" />It's as if the inline handler is wrapped in a with(this). Referencing a property name that exists on the element object, or in the element's prototype chain, will result in that property value being referenced.
So, an inline handler like
<input onchange="somestr">
is interpreted something like
// assume this refers to that input element:
with (this) {
eval(somestr);
}
Note that event is in a different category - it's not a property of the element, it's a global window.event.
function someFunction(id, onclick, children, clientTop) {
console.log(id); // someId
console.log(onclick); // note, this is null, not undefined!
console.log(children); // length 0, but still an HTMLCollection
console.log(clientTop); // 2
}<input type="text" value="Hello" onchange="someFunction(id, onclick, children, clientTop);" id="someId" />function someFunction(id, onclick, children, clientTop) {
console.log(id); // someId
console.log(onclick); // note, this is null, not undefined!
console.log(children); // length 0, but still an HTMLCollection
console.log(clientTop); // 2
}<input type="text" value="Hello" onchange="someFunction(id, onclick, children, clientTop);" id="someId" />answered Jan 1 at 1:24
CertainPerformanceCertainPerformance
86.7k154774
86.7k154774
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%2f53992456%2fwhat-all-information-we-can-pass-as-parameters-to-inline-event-handlers%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
Yes, it is possible to get id as
event.target.idbut I am curious as where is it documented that we can passIdof element also?– javanoob
Jan 1 at 1:19
developer.mozilla.org/en-US/docs/Web/API/Element
– ecg8
Jan 1 at 1:24