0

I want to update a value of one field using values of other fields with spaces. If value is null, space shouldn't be added. What logic should I use as I can't use IF in value set.

EXAMPLE

document.getElementById("target").value= Constant+if(name){" "+this.value}+if(age){" "+this.value}+if(height){" "+this.value};
5
  • I am using required field so at the end it won't make a difference just want to find a way to learn. Commented Sep 8, 2014 at 15:20
  • You can't use an if statement inside an expression. You are looking for the conditional operator. Commented Sep 8, 2014 at 15:21
  • I know and I just meant the logic. I said I can't use IF already. Commented Sep 8, 2014 at 15:36
  • why not put your code in a function where you CAN use if? Commented Sep 8, 2014 at 15:42
  • If you are interested, there are frameworks out there that can do this type of thing for you through binding. Checkout knockout.js, angular.js, backbone.js Commented Sep 8, 2014 at 16:31

2 Answers 2

1

Put your string formatting code in a function where you can use if statements.

document.getElementById("target").value = formatValue();

function formatValue(){
    var formattedString = Constant;

    if(name){
        formattedString += " " + name;
    }

    if(age){
        formattedString += " " + age:
    }

    if(height){
        formattedString += " " + height:
    }

    return formattedString;
}

If you prefer not to have a named function, use a self-invoking anonymous function:

document.getElementById("target").value = (function(){
    var formattedString = Constant;

    if(name){
        formattedString += " " + name;
    }

    if(age){
        formattedString += " " + age:
    }

    if(height){
        formattedString += " " + height:
    }

    return formattedString;
})();
Sign up to request clarification or add additional context in comments.

2 Comments

I know you can use another function for formatting ;) That wasn't the point tho. Thanks anyway!
@Odin You can use a self-invoking anonymous function instead.
1

What you're looking for is called a ternary operator. A boolean condition followed by ? with the true value to return and after : the false value to return.

console.log((name ? name : '') + (age ? age : ''));

For what you're doing though, I wouldn't concatenate a bunch of ternaries in this way. Make an array and then join.

var parts = [];
if (name) {
  parts.push(name);
}
if (age) {
  parts.push(age);
}
// etc. etc.

console.log(parts.join(' '));

If you can use Array.filter (newer browsers), you can simplify your array, not needing all the if statements: http://www.devign.me/javascript-tip-remove-falsy-items-out-of-an-array

2 Comments

I am stuck in the same problem as before, that I have extra spaces. For example if name is not there I will have constant+' '+' '+age+' '+height.
@Odin Not if you use the join method. And, you can always add spaces after the variables above. I just didn't for simplicity so I could explain a ternary to you.

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.