0

I am new to coding, but have been reading about DRY - Don't Repeat Yourself.

I have a JavaScript if/else statement that does not fit the DRY approach, but I am unable to workout how to write the JavaScript if/else statement so that the contents are not repeated.

I am hoping some one smarter than me can show me.

Here is my code:

    {% if user.get_profile.subscription_category == '00' %}

        $('#id_name_details_prefix_title').addClass('kmw-disabled');

        $('#id_name_details_first_name').addClass('kmw-disabled');

        $('#id_name_details_middle_name').addClass('kmw-disabled');

        $('#id_name_details_last_name').addClass('kmw-disabled');

        $('#id_name_details_suffix_title').addClass('kmw-disabled');

    {% else %}

        {% if user.get_profile.display_virtual_keyboard %}

            $('#id_name_details_prefix_title').removeClass('kmw-disabled');

            $('#id_name_details_first_name').removeClass('kmw-disabled');

            $('#id_name_details_middle_name').removeClass('kmw-disabled');

            $('#id_name_details_last_name').removeClass('kmw-disabled');

            $('#id_name_details_suffix_title').removeClass('kmw-disabled');

        {% else %}

            $('#id_name_details_prefix_title').addClass('kmw-disabled');

            $('#id_name_details_first_name').addClass('kmw-disabled');

            $('#id_name_details_middle_name').addClass('kmw-disabled');

            $('#id_name_details_last_name').addClass('kmw-disabled');

            $('#id_name_details_suffix_title').addClass('kmw-disabled');

        {% endif %}

    {% endif %}

The contents of the 1st if are repeated in the else of the 2nd if statement - I am hoping that I only have to write the repeated contents once.

3
  • That's not JavaScript. Are you generating the code on the server or is that some kind of templating language? Anyway, this might be a better question for Code Review. Commented Dec 26, 2014 at 8:30
  • @Juhana It could be a Nunjucks template. Commented Dec 26, 2014 at 8:31
  • oh yeah - its django template language but in js, however the dry principle is the same. Commented Dec 26, 2014 at 8:31

3 Answers 3

3

You can put your selectors in an array

var selectors = [#id_name_details_prefix_title', '#id_name_details_first_name' ...];

and then add/remove class

$(selectors.join(',')).addClass('kmw-disabled')
Sign up to request clarification or add additional context in comments.

Comments

0

Break it up into functions that isolate what's going on.

{% if something %}
    functionThathAddsClass();
{% else %}
    {% if something_else %}
        functionThatRemovesClass();
    {% else %}
        functionThathAddsClass();
    {% endif %}
{% endif %}

You will run into the problem of creating two global functions in this case, but that's a different issue.

I like to make, what I personally call, CSS Guards. A CSS Guard is basically just an object that has functions that pokes and prods at the DOM.

var NameDetailsGuard = {
    functionThathAddsClass = function () {
        $('#id_name_details_prefix_title').addClass('kmw-disabled');
        $('#id_name_details_first_name').addClass('kmw-disabled');
    },
    functionThatRemovesClass = function () {
        $('#id_name_details_prefix_title').removeClass('kmw-disabled');
        $('#id_name_details_first_name').removeClass('kmw-disabled');
    }
};

and you use it like this

NameDetailsGuard.functionThatAddsClass();

An added benefit to this is that if you expand on this concept you can start doing simpler DOM node caching within this object so that instead of querying for the selector everytime you'll retain a reference to it and manipulate that.

Comments

0

You can add some class to needed elements (in HTML markup) and select by that class.

For example, add category-zero and virtual-keyboard classes and use following:

{% if user.get_profile.subscription_category == '00' %}

    $('.category-zero').addClass('kmw-disabled');

{% else %}

    {% if user.get_profile.display_virtual_keyboard %}

        $('.virtual-keyboard').removeClass('kmw-disabled');

    {% else %}

        $('.virtual-keyboard').addClass('kmw-disabled');

    {% endif %}

{% endif %}

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.