1

Problem

I've combined three separate pieces of code i've been using and am having trouble closing the braces and parentheses. Looking at the code i suspect it may not be correct also, although i'm not entirely sure as still in the process of learning JavaScript.

 $('#form1').submit(function(e) {
        var currentForm = this;
        e.preventDefault();
        bootbox.confirm("Are you sure?", function(result) {

            if (result) {

                console.log('Form Submitted'); //Debug line
        $.ajax({
            type: 'POST',
            url: 'indextest4.php',
            data: $("#form1").serialize(),
            error: function () {
                console.log('Ajax Error');}, //Debug Line

            success: function (response) {

                console.log('Ajax Success'); //Debug Line

                $('.fConfirm').css({
                display:"inline", 
                height: 680, 
                width: 940
                });

                console.log("After CSS change");

            }
        });

    });

Also if some kind soul could perhaps tell me a rule of thumb of how i could work out when you close using?

}

})

});

Dan Cundy
  • 2,649
  • 2
  • 38
  • 65
  • 2
    Any decent coding-oriented text editor/IDE will do parentheses/bracket matching for you. – Pointy Mar 11 '14 at 23:10
  • I'm using Dreamweaver, which just throws errors at you and not possible solutions. – Dan Cundy Mar 11 '14 at 23:12
  • There's one word in my original sentence carrying significant importance. – Pointy Mar 11 '14 at 23:14
  • Decent? What would you suggest instead? – Dan Cundy Mar 11 '14 at 23:18
  • `});` is often called a "sad javascripter smiley". There's nothing you can do with numerous braces in JS. Though I recommend using some indentation style, so that your braces are aligned on the left side. – polkovnikov.ph Mar 11 '14 at 23:19
  • @Pointy Got it, got: **decent** : D Haha, no, do you not get highlighting on the matching braces and parentheses in Dreamweaver? Other than that try Sublime Text...(Although I wrote a steamy e-mail about only having small, unseeable dotted lines under the matches). – loveNoHate Mar 11 '14 at 23:19
  • @DanCundy He means something like Notepad++, Eclipse, Intellij Idea. Even CodeMirror editor (which is just an editor control for Web) has such functionality built in. – polkovnikov.ph Mar 11 '14 at 23:20
  • 1
    `}` closes a function definition or a conditional block. `)` closes a set of function arguments or an expression. `})` are used when it's both the end of a function definition and the end of function arguments (which is the case when passing an anonymous function as the last argument to a function). The `})` isn't anything special, just the combination of the two previous rules. – jfriend00 Mar 11 '14 at 23:28
  • Also, some editors can navigate over `TODO` and `FIXME` in comments, so you can add one of those instead of `Debug line` to make them easier to remove. – polkovnikov.ph Mar 11 '14 at 23:28
  • @dollarVar No Dreamweaver doesn't have a great syntax editor, Object-orientated IDE's literally take a dump on Dreamweaver. Going to give Notepad++ a go! – Dan Cundy Mar 11 '14 at 23:29
  • Yeah, that's cool, actually got better highlighting than (my loved) SublimeText. ;) – loveNoHate Mar 11 '14 at 23:30
  • 2
    Also, if your editor isn't helping you, you can always paste a block of code into a jsBeautifier and it will often help you a lot too because it makes the indentation match the bracing often showing you were there are faults. I use [this one](http://jsbeautifier.org/). – jfriend00 Mar 11 '14 at 23:36
  • Btw, http://jsfiddle.net also got highlighting... – loveNoHate Mar 11 '14 at 23:43
  • @dollarVar is the SublimeText worth the cost? 70 USD seems alot! – Dan Cundy Mar 11 '14 at 23:46
  • Yeah, well, I wrote them I won't buy it before they fix me the highlight thing, haha. Actually I am just about to find the right plugin somewhere, should work. That said, you do not *have* to buy it...all the features are fully available. But you should. ;) Try some editors out and then choose. BTW, if you are really brave use http://www.vim.org/. But then you have to learn basically a programming language to use it. – loveNoHate Mar 11 '14 at 23:52
  • 1
    Ok, found it: http://stackoverflow.com/a/10372059/2672018. No excuse anymore (and if you make money with a product that costs, don't use it for free). – loveNoHate Mar 11 '14 at 23:58
  • @dollarVar, Christ the website of Vim put me off alone, never mind your comment. downloaded Notepad++ and going to try Sublime, it's looks very impressive i'll give you that! What do you use it for primarily? Writing your code straight into it? – Dan Cundy Mar 12 '14 at 00:04
  • Yeah, writing my code is the one thing, then I had to find matching brackets in like thousand lines of code I had to maintain, then the enerving sublime (so to say) highlighting ****** me off. But when you have like a whole folder (with subfolders) you can just drop it into the side bar...et voilá. – loveNoHate Mar 12 '14 at 00:15

5 Answers5

2

Correct indentation helps a lot here since everything will line up nicely after. Here is what I think you are looking for:

$('#form1').submit(function(e) {
    var currentForm = this;
    e.preventDefault();
    bootbox.confirm("Are you sure?", function(result) {
        if (result) {
            console.log('Form Submitted'); //Debug line
            $.ajax({
                type: 'POST',
                url: 'indextest4.php',
                data: $("#form1").serialize(),
                error: function () {
                    console.log('Ajax Error');
                }, //Debug Line

                success: function (response) {

                    console.log('Ajax Success'); //Debug Line

                    $('.fConfirm').css({
                        display:"inline", 
                        height: 680, 
                        width: 940
                    });

                    console.log("After CSS change");

                } // success: ...
            }); // $.ajax ...
        } // if (result) ...
    }); // bootbox.confirm ...
}); // $('#form1').submit ...

I added comments on all closing braces at the end to show what they correspond to.

Andrew Clark
  • 202,379
  • 35
  • 273
  • 306
2

As other pointed out, the key here is to indent your code, and use a good editor:

enter image description here

Not a rule of thumb but one tip is to add a tab space as you nesting block of code.

Most editor will let you ident the code by selecting it and using keyboard shortcuts:

to the left:

Shift + Tab

to the right:

Tab

meda
  • 45,103
  • 14
  • 92
  • 122
  • This looks great, may i ask what program you are using? – Dan Cundy Mar 11 '14 at 23:35
  • Notepad ++ http://notepad-plus-plus.org/ most programmer like it because it's light – meda Mar 11 '14 at 23:36
  • About the `tabs`... http://nodejsreactions.tumblr.com/post/55007912035/when-i-get-a-pull-request-that-uses-tabs-instead-of. The node.js community somehow loves spaces. – loveNoHate Mar 11 '14 at 23:40
1

Your last line should be two curly braces. As with the comments, get a good editor like Notepad++ they will show you the open and close braces, tags, etc

Byron Claiborne
  • 217
  • 3
  • 12
1

This should be correct:

$('#form1').submit(function(e) {
    var currentForm = this;
    e.preventDefault();
    bootbox.confirm("Are you sure?", function(result) {

        if (result) {

            console.log('Form Submitted'); //Debug line
            $.ajax({
                type: 'POST',
                url: 'indextest4.php',
                data: $("#form1").serialize(),
                error: function () {
                    console.log('Ajax Error');}, //Debug Line

                success: function (response) {

                    console.log('Ajax Success'); //Debug Line

                    $('.fConfirm').css({
                        display:"inline", 
                        height: 680, 
                        width: 940
                    });

                    console.log("After CSS change");

                }
            });
        }
    });
});
Matúš Dúbrava
  • 771
  • 5
  • 18
1

This may not fix all of your problems, but it does fix the opening and closing braces.

    $('#form1').submit(function(e) {  //start form submit handler
        var currentForm = this;
        e.preventDefault();
        bootbox.confirm("Are you sure?", function(result) {  //start bootbox.confirm callback
            if (result) {                      //start if block
                console.log('Form Submitted');
                $.ajax({                        //start ajax call
                    type: 'POST',
                    url: 'indextest4.php',
                    data: $("#form1").serialize(),
                    error: function () {            //start ajax error callback
                        console.log('Ajax Error');
                    },                              //end ajax error callback

                    success: function (response) {  //start ajax success callback
                        console.log('Ajax Success'); 
                        $('.fConfirm').css({         //start css change
                            display:"inline", 
                            height: 680, 
                            width: 940
                        });                          // end css change
                        console.log("After CSS change");
                    }                                //end ajax success callback
               });   //end ajax call
           }    //end if block
       });  //end bootbox.confirm
   }); //end form submit handler

It just takes a little practice, but the basic rule of thumb is to close everything you open. Whether it's a '(' or a '{' .

Gisheri
  • 1,645
  • 2
  • 18
  • 27