/    Sign up×
Community /Pin to ProfileBookmark

Hi. I have configured my first ever js and to be honest. I can just about read it let alone understand how to fix the error. Basically I want to change details in a field, on pressing the enter key submit the form but so a confirmation dialog before the form actually submits. If all is good submit the actual form.

here is my js

[CODE]
<script>
$(document).ready(function() {
$(document).keyup(function(event) {
if (event.keyCode == 13) {

submitHandler: function( ){
answer = confirm(“Are you sure you want to submit?”);
if (answer == true){
$(“#v21frm”).submit();
}
else{
return false;
}
}

}
})
});
</script>
[/CODE]

Now to issue I’m having. Which is the form is updating like it should be but the confirmation box is not appearing. Its just going straight through.

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@Druid_McGrewauthorSep 06.2011 — Ok I have had the mistakes pointed out to me so here is the updated code but the confirmation dialog is still not appearing. The form just updates automatically.

<i>
</i>$(function() {
// constant data
var
CONFIRM_MESSAGE = "Are you sure you want to submit?",
FORM_ID = "#v21frm",
ENTER_KEY = 13;

<i> </i>// on keyup
<i> </i>$(document).keyup(function (event) {

<i> </i> var answer;

<i> </i> // if enter key released
<i> </i> if (event.keyCode === ENTER_KEY) {

<i> </i> // confirm the form submission
<i> </i> answer = confirm(CONFIRM_MESSAGE);

<i> </i> // on confirmation
<i> </i> if (answer) {
<i> </i> // submit the form
<i> </i> $(FORM_ID).submit();
<i> </i> }

<i> </i> return false;
<i> </i> }
<i> </i>});
});
Copy linkTweet thisAlerts:
@pactor21Sep 06.2011 — Try == instead of ===. (2 equals in the place of 3 equals.)
Copy linkTweet thisAlerts:
@DanInMASep 06.2011 — honeslty there are quite a few things wrong here.

  • 1. I'm pretty sure you are trying to fire this whenever the form is submitted, if so then this is not a good way to do it.


  • 2. when you declare JS variables each one must start with var , not just the first one.


  • 3. the variables you added don't help or hurt


  • 4. like he said, use == not === . == compares the basic result while === compares the result and the result type as well.

  • - typically you should only use === when comparing true/false or numbers ( not numbers in string format)


  • try this and see if it is what you are shooting for:

    [CODE]

    $(function () {
    $("#v21frm").submit(function (event) {
    event.preventDefault();
    var answer;
    answer = confirm("Are you sure you want to submit?");
    // on confirmation
    if (answer === true) {
    // submit the form
    return true;
    } else {
    return false;
    }
    });
    });
    [/CODE]
    Copy linkTweet thisAlerts:
    @KorSep 07.2011 — 
    - typically you should only use === when comparing true/false
    [/quote]

    Not by all means. If you need a comparison with a Boolean, that means the variable itself should have as value a Boolean (or should be treated as a Boolean), thus the comparison operator is redundant. It is enough to write the statement as:
    <i>
    </i>if (answer){
    //...
    }
    Copy linkTweet thisAlerts:
    @DanInMASep 07.2011 — ty Kor, excellent point.
    Copy linkTweet thisAlerts:
    @DanInMASep 07.2011 — super simplified:

    [CODE]$(function () {
    $("#v21frm").submit(function (event) {
    event.preventDefault();
    var answer = confirm("Are you sure you want to submit?");
    if (answer) {
    // submit the form
    return true;
    } else {
    return false;
    }
    });
    });[/CODE]
    Copy linkTweet thisAlerts:
    @KorSep 08.2011 — Hyper simplified ? :
    <i>
    </i>$(function () {
    $("#v21frm").submit(function (event) {
    event.preventDefault();
    return confirm("Are you sure you want to submit?");
    });
    });
    Copy linkTweet thisAlerts:
    @DanInMASep 08.2011 — well played sir. well played. very nice. here i saved some more letters hahhaha:

    $(function () {
    $("#v21frm").submit(function (e) {
    e.preventDefault();
    return confirm("Are you sure you want to submit?");
    });
    });
    Copy linkTweet thisAlerts:
    @KorSep 08.2011 — Frankly said, the whole code is useless. As a user, when I press a submit button, I am pretty [I]sure[/I] I want to submit, otherwise why should I press that button? :rolleyes:

    In other words, the Ultimate simplified version is just to get rid of the whole function ?

    If so, I don't think there is more be simplified ?
    ×

    Success!

    Help @Druid_McGrew spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 4.27,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...