jumps4fun
jumps4fun

Reputation: 4132

Why is my javascript function completely ignored? (Very simple example)

I have two buttons. Save and undo. They execute a javascript, and in the end they call resetButtons() which is meant to disable the buttons until any other change in the users input occurs. This code has worked for over a year now, but while implementing some new features, this one has broken.

Here is a direct snippet of code:

function undo(){
    var r = confirm("Are you sure you want to cancel your inputs?");
    if (r == true) {
        setLoadedValues();  //calls in the cached previous values
        updateAllBoxValues();  //updates view
        console.log("before resetting buttons");
        resetButtons();
        console.log("after resetting buttons");
    }
}

function resetButtons(){
    console.log("resetting buttons");
    $(".save").addClass("disabled");
    $('.save').attr('disabled', 'disabled');
    $(".undo").addClass('disabled');
    $('.undo').attr('disabled', 'disabled');
    console.log("done resetting buttons");
}

This gives the following output:

before resetting buttons
after resetting buttons

I have suspected some wrong formatting with brackets, but this is a direct snippet of code, and if undo() works, so should resetButtons(), right? Also, I should have been given a undefined function, or an unexpecetd end of input or something similar if that was the case. The call to resetButtons is also ignored when I click the save button. I am lost. Help?

Upvotes: 2

Views: 165

Answers (3)

Vlad Pintea
Vlad Pintea

Reputation: 853

Try triggering the resetButton() function in console, if there's an issue it should return it. Testing it in a random console gives all the logs it should, but I can't vouch for the selectors. The error is probably elsewhere in the code.

Upvotes: 1

Barmar
Barmar

Reputation: 780974

You have a duplicate function definition. Rename one of them.

Upvotes: 3

Nicholas Valbusa
Nicholas Valbusa

Reputation: 339

Your code seems fine, you must have some other error somewhere else (like a function named like those two).

By the way, I suggest using jQuery's "prop" method to change the disabled attribute:

$('foo').prop('disabled', true);

$('foo').prop('disabled', false);

Upvotes: 1

Related Questions