Pass element ID to Javascript function

后端 未结 5 1141
南旧
南旧 2020-12-01 16:37

I have seen many threads related to my question title.

Here is HTML Codes :

相关标签:
5条回答
  • 2020-12-01 16:38

    The problem for me was as simple as just not knowing Javascript well. I was trying to pass the name of the id using double quotes, when I should have been using single. And it worked fine.

    This worked:

    validateSelectizeDropdown('#PartCondition')
    

    This did not:

    validateSelectizeDropdown("#PartCondition")
    

    And the function:

        function validateSelectizeDropdown(name) {
        if ($(name).val() === "") {
             //do something
        }
    }
    
    0 讨论(0)
  • 2020-12-01 16:44

    This'll work:

    <!DOCTYPE HTML>
    <html>
        <head>
            <script type="text/javascript">
                function myFunc(id)
                {
                    alert(id);
                }
            </script>
        </head>
    
        <body>
            <button id="button1" class="MetroBtn" onClick="myFunc(this.id);">Btn1</button>
            <button id="button2" class="MetroBtn" onClick="myFunc(this.id);">Btn2</button>
            <button id="button3" class="MetroBtn" onClick="myFunc(this.id);">Btn3</button>
            <button id="button4" class="MetroBtn" onClick="myFunc(this.id);">Btn4</button>
        </body>
    </html>
    
    0 讨论(0)
  • 2020-12-01 16:46

    you can use this.

    <html>
        <head>
            <title>Demo</title>
            <script>
                function passBtnID(id) {
                    alert("You Pressed:  " + id);
                }
            </script>
        </head>
        <body>
            <button id="mybtn1" onclick="passBtnID('mybtn1')">Press me</button><br><br>
            <button id="mybtn2" onclick="passBtnID('mybtn2')">Press me</button>
        </body>
    </html>
    
    0 讨论(0)
  • 2020-12-01 16:48

    Check this: http://jsfiddle.net/h7kRt/1/,

    you should change in jsfiddle on top-left to No-wrap in <head>

    Your code looks good and it will work inside a normal page. In jsfiddle your function was being defined inside a load handler and thus is in a different scope. By changing to No-wrap you have it in the global scope and can use it as you wanted.

    0 讨论(0)
  • 2020-12-01 16:57

    In jsFiddle by default the code you type into the script block is wrapped in a function executed on window.onload:

    <script type='text/javascript'>//<![CDATA[ 
        window.onload = function () {
            function myFunc(id){
                alert(id);     
            }
        }
    //]]>  
    </script>
    

    Because of this, your function myFunc is not in the global scope so is not available to your html buttons. By changing the option to No-wrap in <head> as Sergio suggests your code isn't wrapped:

    <script type='text/javascript'>//<![CDATA[ 
        function myFunc(id){
           alert(id);     
        }
    //]]>  
    </script>
    

    and so the function is in the global scope and available to your html buttons.

    0 讨论(0)
提交回复
热议问题