Function is undefined error

I'm trying to call a function in onchange given to a text box. Whenever I focusout of a text box, the function should be called. But I'm getting myFunction(function name) is undefined. This is what I have done:

var c = 0;
    jQuery(document).ready(function() {
        jQuery("#table_projection_value").dataTable({
                "sAjaxSource": "includes/inc-projection-db.php?mode=projection_dataTable",
                "bDestroy": true,
                "bPaginate": false,
                "bInfo": false,
                "bFilter": false,
                "bSort": false,
                "aoColumnDefs": [
                    {
                        "aTargets": [0],
                        "mRender": function(data, type, row) {
                            return data + '<input type="hidden" class="user_id" name="user_id[]" id="user_id" value="' + row[4] + '">';
                        }
                    },
                    {
                        "aTargets": [1],
                        "mRender": function(data, type, row) {
                            return '<input type="text" onchange="myFunction();" class="form-control text-right projected_value" name="projected_value[]" id="projected_value_' + c + '_' + data + '" >';
                        }
                    }
                ],
                "fnCreatedRow": function(nRow, aData, iDisplayIndex, iDisplayIndexFull){
                    c = c + 1;
                }
            });

        function myFunction(){
            $(":text").blur(function() {
            alert("**");
            var element=$(this); // you can get the element here
            });


            $(":text").focusout(function() {
            alert(this.id + " focus out");
            });
        }   
});

What should I do?

Answers 1

  • Try writing your function out of jQuery(document).ready(function() {...})

    jQuery(document).ready(function() {...});
    function myFunction(){
                $(":text").blur(function() {
                alert("**");
                var element=$(this); // you can get the element here
                });
    
    
                $(":text").focusout(function() {
                alert(this.id + " focus out");
                });
            }   
    

Related Articles