Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Cannot figure out why jquery code says missing bracket

I wrote some jquery code and it worked before, however now the console is throwing an error saying missing bracket before the else;

$(function(){
    $('[name="delete"],[name="process"]').click(function() {
        var title = $('[name="title"]').val();
        var desc = $('[name="desc"]').val();
        var link = $('[name="link"]').val();
        var date = $('[name="date"]').val();
        var img_link = $('[name="img_link"]').val();
        if((this).attr("name") == 'delete'){
            $.ajax({
                url: '/',
                data: $('form').serialize(),
                type: 'POST',
                success: function(response) {
                console.log(response);
            },
                error: function(error) {
                console.log(error);
            }
        }else if((this).attr("name") == 'process'){
            $.ajax({
                url: '/',
                data: $('form').serialize(),
                type: 'POST',
                success: function(response) {
                console.log(response);
            },
                error: function(error) {
                console.log(error);
            }


        }



        });

    });

});

2 Answers

Hey Mayur Pande , Try this

$(function(){

    $('[name="delete"],[name="process"]').click(function() {
        var title = $('[name="title"]').val();
        var desc = $('[name="desc"]').val();
        var link = $('[name="link"]').val();
        var date = $('[name="date"]').val();
        var img_link = $('[name="img_link"]').val();
        if((this).attr("name") == 'delete'){
            $.ajax({
                url: '/',
                data: $('form').serialize(),
                type: 'POST',
                success: function(response) {
                console.log(response);
                },
                error: function(error) {
                console.log(error);
                }
            });


        }else if((this).attr("name") == 'process'){
            $.ajax({
                url: '/',
                data: $('form').serialize(),
                type: 'POST',
                success: function(response) {
                console.log(response);
                },
                error: function(error) {
                console.log(error);
                }
            });

    }

});

});

thanks managed to figure it out yesterday, but not sure how it was working before hand!

I think You're missing a curly bracket, closing bracket and semicolon after your last curly bracket before 'else if'. I may be wrong, I'm new to the topic. Cheers.