1

The below piece of code working but to simplify the code, how can we use the ternary operator ?

function fnOnChangeType() {

    if (document.RequestHistoryForm.strMessageType.value=="All") {
        document.getElementById("selectMessageView").disabled=false;
    } else {
        document.getElementById("selectMessageView").disabled=true;
    }
}

function fnOnChangeView() {
    if (document.RequestHistoryForm.strMessageView.value=="") {
        document.getElementById("selectMessageType").disabled=false;
    } else {
        document.getElementById("selectMessageType").disabled=true;
    }
}

5 Answers 5

5

No need of a ternary here :

function fnOnChangeType() { 
    document.getElementById("selectMessageView").disabled = document.RequestHistoryForm.strMessageType.value !== "All";
}

function fnOnChangeView() {
    document.getElementById("selectMessageType").disabled = document.RequestHistoryForm.strMessageView.value !== "";
}
Sign up to request clarification or add additional context in comments.

Comments

2

Syntax for ternary operation in JS :

 var statement = expression1 ? value1 : value2 ; 
    // If expression is true,then return value1, 
    // otherwise return value2; 

Try :

document.getElementById("selectMessageView").disabled = document.RequestHistoryForm.strMessageType.value=="All" ? false : true;

2 Comments

And shorter: document.getElementById("selectMessageView").disabled = !document.RequestHistoryForm.strMessageType.value == "All"; OR document.getElementById("selectMessageView").disabled = document.RequestHistoryForm.strMessageType.value != "All";
@SleepyPanda : Absolutely. This is a short and good solution. Since the question explicitly mentioned about ternary operator , I provided a solution with ternary operation
2

I think you can shortened it by doing this;

function fnOnChangeType() {
    document.getElementById("selectMessageView").disabled = document.RequestHistoryForm.strMessageType.value !== "All";
}
function fnOnChangeView() {
    document.getElementById("selectMessageType").disabled = document.RequestHistoryForm.strMessageView.value !== "";
}

2 Comments

Instead of negating an equality operation, you can just use an inequality one i.e. !==
yeah or you can copy paste my answer now :D
1

Another approach, which I probably wouldn't use for only two sets like this, but would if there are three or more, is to write a generic version to reuse:

function enableFieldOnValue(fieldToEnable, testField, value) {
  document.getElementById(fieldToEnable).disabled = 
    document.RequestHistoryForm[testField].value == value
}

enableFieldOnValue("selectMessageView", "strMessageType", "All")
enableFieldOnValue("selectMessageType", "strMessageView", "")

Comments

0

I think the best you can get is this:

fnOnChangeType = ()=>
    document.getElementById("selectMessageView").disabled = 
        document.RequestHistoryForm.strMessageType.value != "All"
fnOnChangeView = ()=>
    document.getElementById("selectMessageType").disabled =    
        document.RequestHistoryForm.strMessageView.value != ""

The use of the ternary operator would be worst in this case, because you would only lengthen the function with an unecessary ? false : true suffix, when if you just invert the condition you can simplify the line. Take a look:

fnOnChangeType = ()=>
    document.getElementById("selectMessageView").disabled = 
        (document.RequestHistoryForm.strMessageType.value == "All" ? false : true;
fnOnChangeView = ()=>
    document.getElementById("selectMessageType").disabled =    
        document.RequestHistoryForm.strMessageView.value == "" ? false : true;

You make it more readable if you put the objects in variables:

var select = document.getElementById("selectMessageView"),
    requestHistoryForm = document.RequestHistoryForm;

fnOnChangeType = ()=> select.disabled = requestHistoryForm.strMessageType.value != "All";
fnOnChangeView = ()=> select.disabled = requestHistoryForm.strMessageView.value != "";

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.