jeudi 23 avril 2015

Detecting if an HTML checkbox was selected

I have created the following jQuery script to check if my "chkboxAdd" checkbox has been clicked or not.

    (function ( $ ){
        $.fn.myfunction = function(){
            $("input[type='checkbox']").click(function() 
            {      
                if($(this).attr('name') == 'chkboxAdd' && $(this).attr('checked') == true   )
                {

                    alert('Checkbox add is checked!');
                } 
                else 
                {
                    alert('Checkbox add is NOT checked!');
                 }
            });
        };   
    })(jQuery);

However after clicking the "chkboxAdd" checkbox, the alert prompt always go on the else clause instead of the if clause and prompts:

            Checkbox add is NOT checked!

I have included the relevant HTML snippets for reference. Thank you in advance.

<script>
     $(document).ready(function() 
     {
         $('#chkBoxTable').myfunction();
     } ); 
</script>

                    <table id="chkBoxTable" cellpadding="10" cellspacing="1">
                        <thead>
                                <tr>
                                    <th><strong>Page ID</strong></th>
                                    <th><strong>Page Name</strong></th>
                                    <th><strong>Action</strong></th>
                                </tr>
                        </thead>
                    <?php
                        <tbody>
                                <tr>
                                    <td><?php echo $row["PAGEID"]; ?></td>
                                    <td><?php echo $row["PAGENAME"]; ?></td>
                                    <td><input type="checkbox" name="chkboxAdd" value="add">Add</td>
                                </tr>
                        </tbody>
                    </table>




Aucun commentaire:

Enregistrer un commentaire