What all information we can pass as parameters to inline event handlers?












1















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>









share|improve this question

























  • 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
















1















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>









share|improve this question

























  • 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














1












1








1








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>









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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



















  • 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

















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












1 Answer
1






active

oldest

votes


















2














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.






share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    2














    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.






    share|improve this answer




























      2














      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.






      share|improve this answer


























        2












        2








        2







        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.






        share|improve this answer













        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" />






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 1 at 1:24









        CertainPerformanceCertainPerformance

        86.7k154774




        86.7k154774
































            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Mossoró

            Error while reading .h5 file using the rhdf5 package in R

            Pushsharp Apns notification error: 'InvalidToken'