9

When I click on the button, the function is not called

 2 years ago
source link: https://www.codesd.com/item/when-i-click-on-the-button-the-function-is-not-called.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

When I click on the button, the function is not called

advertisements

This question already has an answer here:

  • Why does jQuery or a DOM method such as getElementById not find the element? 6 answers

I'm new to JavaScript and JQuery, and I'm trying to figure out why my function doesn't get called when I press the button. My only clue is that the Firefox Console is showing TypeError: document.getElementById(...) is null. I found this example, which says "document.write will overwrite the entire DOM if it's called after the document has finished being parsed" and that's why it's happening. I took a look at my code, and I moved the button into my div tag and it's still happening. I also tried moving the button into it's own div tags. Compiler didn't seem to like syntax when I tried adding html tags where the button is. I'm not sure what I'm supposed to do to fix this.

I'm following this example for the function call on button click. Hopefully I'm applying it ok to my project.

This is my code:

 <!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.

-->
<html>
    <head>
        <title>jQuery Michele Project</title>
        <link href="css/skins/polaris/polaris.css" rel="stylesheet">
        <link href="css/skins/all.css" rel="stylesheet">
        <link href="css/demo/css/custom.css" rel="stylesheet">
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script type="text/javascript" src="js/jquery-1.11.0.js"></script>
        <script type="text/javascript" src="js/jquery.ui.core.js"></script>
        <script type="text/javascript" src="js/jquery.ui.widget.js"></script>
        <script src="js/icheck.js"></script>

        <script type="text/javascript">
            $(document).ready(function(){
                $('.input').iCheck({
                    checkboxClass:'icheckbox_polaris',
                    radioClass:'iradio_polaris',
                    increaseArea:'10%'
                });
            });
        </script>

        <script type="text/javascript">
            // Returns an array with values of the selected (checked) checkboxes in "frm"
            function getSelectedChbox(frm) {
                // JavaScript & jQuery Course - http://coursesweb.net/javascript/
                var selchbox = [];        // array that will store the value of selected checkboxes

                // gets all the input tags in frm, and their number
                var inpfields = frm.getElementsByTagName('input');
                var nr_inpfields = inpfields.length;

                // traverse the inpfields elements, and adds the value of selected (checked) checkbox in selchbox
                for(var i=0; i<nr_inpfields; i++) {
                    if(inpfields[i].type == 'checkbox' && inpfields[i].checked == true)
                        selchbox.push(inpfields[i].value);
                }
                return selchbox;
            }

            document.getElementById('btntest').onclick = function() {
                var selchb = getSelectedChbox(this.form);
                alert(selchb);
            }
        </script>
        <style type="text/css">
            ul {list-style-type: none}
            img {padding-right: 20px; float:left}
            #infolist {width:500px}
        </style>
    </head>
    <body>
        <form>
            <div class="skin skin-line">
                <div class="arrows">
                    <div class="top" data-to="skin-flat"></div>
                    <div class="bottom" data-to="skin-polaris"></div>
                </div>
            </div>
            <div class="skin skin-polaris">
                <div class="arrows">
                    <div class="top" data-to="skin-line"></div>
                    <div class="bottom" data-to="skin-futurico"></div>
                </div>
                <h3>Select Items for Column Headings</h3>
                <dl class="clear">
                    <dd class="selected">
                        <div class="skin-section">
                            <h4>Live</h4>
                            <ul class="list">
                                <li>
                                    <input tabindex="21" type="checkbox" id="polaris-checkbox-1">
                                    <label for="polaris-checkbox-1">Checkbox 1</label>
                                </li>
                                <li>
                                    <input tabindex="22" type="checkbox" id="polaris-checkbox-2" checked>
                                    <label for="polaris-checkbox-2">Checkbox 2</label>
                                </li>
                                <li>
                                    <input type="checkbox" id="polaris-checkbox-3" >
                                    <label for="polaris-checkbox-3">Checkbox 3</label>
                                </li>
                                <li>
                                    <input type="checkbox" id="polaris-checkbox-4" checked >
                                    <label for="polaris-checkbox-4">Checkbox 4</label>
                                </li>
                            </ul>
                        </div>
                        <script>
                        $(document).ready(function(){
                            $('.skin-polaris input').iCheck({
                                checkboxClass: 'icheckbox_polaris',
                                radioClass: 'iradio_polaris',
                                increaseArea: '20%'
                            });
                        });
                        </script>
                        //$('#checkbox').prop('checked')
                    </dd>
                </dl>
            </div>
            <div id="loading">
                <input type="button" value="Click" id="btntest" />
            </div>
        </form>
    </body>
</html>


The issue is you assign the click handler before the element is available in the DOM. To resolve, you should wrap it in a DOM ready function:

$(function(){
    document.getElementById('btntest').onclick = function() {
        var selchb = getSelectedChbox(this.form);
        alert(selchb);
    }
});

Note: $(function(){ ... }); is a shortcut for $(document).ready(function(){ ... });


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK