0

I have two variables like Question_type and Answer_type in JavaScript.

I have a hidden_field with no value, I want to insert a value to the hidden_field by checking the conditions of Question_typeand Answer_type like the following:

If Question_type == 'A' && Answer_type == 'B' {
    hidden_field = 1;
} else if Question_type == 'A' && Answer_type !== 'B' {
    hidden_field = 2;
} else if Question_type !== 'A' && Answer_type == 'B' {
    hidden_field = 3;
{ else if Question_type !== 'A' && Answer_type !== 'B' {
    hidden_field = 4;
}

How to do this concept by short form or the clean method in JavaScript? Any JSfiddle example would be more appreiciated.

1
  • "How to do this concept by short form..." Are you asking how to shorten this code, or how to set the value of the field? Commented Oct 11, 2012 at 13:57

5 Answers 5

4

Use getElementById to get hidden by Id:

 var hidden_field = document.getElementById('HiddenFieldId');
 hidden_field.value = 1;
Sign up to request clarification or add additional context in comments.

Comments

3

Here's one option to shorten the code:

var isA = Question_type == 'A',
    isB = Answer_type == 'B';

hidden_field =  isA &&  isB ? 4 :
                isA && !isB ? 3 :
               !isA &&  isB ? 2 :
                              1;

Here's another:

if (Question_type == 'A')
    if (Answer_type == 'B')
        hidden_field = 4;
    else
        hidden_field = 3;
else
    if (Answer_type == 'B')
        hidden_field = 2;
    else
        hidden_field = 1;

6 Comments

@Vinay: I updated the answer (at the top) since we really don't need to test the last condition. If the first 3 fail, then the last will always pass, so 0 would never have been set.
yeah here its shows the error for the last statement. 'Uncaught SyntaxError: Unexpected token ( '
@Vinay: Make sure you copied the code correctly. There aren't any SyntaxErrors in the code above.
@Vinay: I prefer the first example if it can be done cleanly. If the code starts becoming a mess or unclear, then I use if statements.
@Vinay: The safe and proper way to do it is to make it a property of window. Like this: window.question_type = "foo". All global variables can be accessed from the window object.
|
1

Modified code: If should be if and conditions must be inside '()'.

if (Question_type == 'A' && Answer_type == 'B') {
     $("#hidden_field").val( 1);
} else if (Question_type == 'A' && Answer_type !== 'B') {
    $("#hidden_field").val(  2);
} else if (Question_type !== 'A' && Answer_type == 'B') {
     $("#hidden_field").val( 3);
{ else if (Question_type !== 'A' && Answer_type !== 'B') {
       $("#hidden_field").val( 4);
}

4 Comments

what does it mean by inside (). I am new to javascript. Please define ?
@MalSu he has jQuery tag in question.
@Vinay if condition1 && condition2 is invalid in JavaScript it should be if(condition1 && condition2).
@Shusl and Rails as well, but he's new to JS. I don't believe showing a jQuery answer to a basic JS problem is the correct approach. Although for completeness the answer should remain.
1

Using ternary operator may help shortening the code:

if (Question_type == 'A') {
    hidden_field.value = Answer_type == 'B' ? 1 : 2;
} else {
    hidden_field.value = Answer_type == 'B' ? 3 : 4;
}

check out this jsFiddle demo

Comments

-1

Give some ID to hidden field, and set it's 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.