1

Will someone please tell me why the first snippet of code doesn't work and the second one does. Is it the issue of variable scoping or any other issue?

jQuery(document).ready(function($) {
    var displayNo = $(".ue-show-success").css('display', 'none');
    var displayYes = $(".ue-show-error").css('display', 'block');

    $(".somebutton").on('click', function() {
        displayNo;
        displayYes.text('some text');
    });
});

The above code doesn't work as expected. But below one does.

jQuery(document).ready(function($) {
    $(".somebutton").on('click', function() {
        $(".ue-show-success").css('display', 'none');
        $(".ue-show-error").css('display', 'block').text('some text');
    });
});

Thanks in advance

0

1 Answer 1

1

This statement creates an object:

var displayNo = $(".ue-show-success").css('display', 'none');

It is not a callable function. You'd have to do something like this:

var displayNo = function() {
    return $(".ue-show-success").css('display', 'none');
}

You'd then call that function with parentheses, like so:

displayNo();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.