9

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

Keyslinger
  • 4,903
  • 7
  • 42
  • 50
  • 3
    Are you trying to do a window.confirm(key)? Where is confirm defined? – sissonb Nov 09 '11 at 03:06
  • 1
    I would guess that 1) something changed `window.confirm` or 2) there is a variable called `confirm`... it wouldn't hurt to see what `confirm` evaluates to. –  Nov 09 '11 at 03:08
  • @Keyslinger Type in "confirm" in the JavaScript console. – Dave Newton Nov 09 '11 at 03:11
  • @DaveNewton see my edit. When I type confirm('test') into the console, the confirm executes fine. – Keyslinger Nov 09 '11 at 03:30
  • Can you do `console.log(confirm)` in the `success` function? If you overwrote confirm in a function scope it wouldn't show up when you test confirm in the console. – Brian Nickel Nov 09 '11 at 04:57

4 Answers4

23

The confirm function is part of the window object.

if (window.confirm(key)){
    console.log(item);
}
sissonb
  • 3,730
  • 4
  • 27
  • 54
  • 1
    Yes, but `window` is the global [[scope]], so something is amiss ;-) –  Nov 09 '11 at 03:10
  • 1
    @pst hmm. what does console.log(confirm) say? – sissonb Nov 09 '11 at 03:13
  • 2
    @sissonb when I put console.log(confirm) into the loop, it printed one of my error messages. On further inspection, it turned out I was using a variable called "confirm" to store messages before they were put into the message object. If you write an answer about not having a variable named "confirm" I'll give you credit! – Keyslinger Nov 09 '11 at 03:42
21

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.

sissonb
  • 3,730
  • 4
  • 27
  • 54
  • 1
    In the code replaced by `//... load 'messages' object with stuff` in my original question I did the following: `confirm = 'The site ' + data.site_with_same_coords.name + ' already exists in the location you have indicated';` – Keyslinger Nov 09 '11 at 18:58
3

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

Hink
  • 1,054
  • 1
  • 15
  • 31
1

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

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

YanXing Ou
  • 49
  • 2