Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript error says "confirm is not a function"

I'm using jQuery with the validation plugin when I submit a form:

$('.frmProject:visible').validate( {

    errorContainer: ".site_details:visible .messageBox1",
    errorLabelContainer: ".site_details:visible .messageBox1 span.messagehere",
    invalidHandler: function(form, validator) {
    },

    rules: {
        site_id: {
            required: true,
        }
    },
    messages: {
        site_id: "Project has no assigned site information. Click the marker on the map at left to specify the site where this project took place."
    },
    submitHandler: function(data) {
        SaveProject();
    }

});

In the submitHandler,

function SaveProject(){
    //... load variables with input contents
    $.ajax({
        url: 'ajax/save_project.php',
        dataType: 'json',
        data: 'id='+id+'&title='+title+'&project='+project+'&sector='+sector+'&volunteer='+volunteer+'&lat='+lat+'&lng='+lng+'&name='+name+'&mun='+mun+'&prov='+prov,
        success: function(data) {
            //... load 'messages' object with stuff
            $.each(messages, function(key, value) {
                if (confirm(key)){
                    console.log(item);
                }
            });
        }
    });
}

When I submit the validated form and it gets to the confirm inside the each loop, I get the error message: "confirm is not a function".

How can I present a message to the user for confirmation?

Edit:

When I type "confirm" into the console I get this:

Screen capture of console output

Inspecting in DOM reveals:

window > confirm() There are no properties to show for this object.

Inspecting in Script takes me to a place in jquery-1.6.2.min.js

like image 201
Keyslinger Avatar asked Nov 09 '11 02:11

Keyslinger


3 Answers

If you assign a variable without var, you are assigning that variable to the global space. In your case there was a string assigned to the confirm variable that overrode the native confirm method.

like image 153
sissonb Avatar answered Nov 04 '22 13:11

sissonb


Defining any html object with attribute id ="confirm" also causes this error

like image 3
Hink Avatar answered Nov 04 '22 12:11

Hink


I use both 'jquery confirm' and 'datatables', in my case, 'datatables' should be imported before 'jquery confirm'.

So, in some case, try ReOrder ........

like image 1
YanXing Ou Avatar answered Nov 04 '22 14:11

YanXing Ou