Add underscore _ to text string

makiz63
Kilo Contributor

I have the following script which is not working, I want to have an underscore after the 'Room" before the var_area is displayed

ie:  Room_ComptuterRoom301

if (loc1) {

desk +="Room" + "_" + 'var_area\n';

}

How do I join the symbol.

1 ACCEPTED SOLUTION

Okay, I see a few things wrong with this. First, loc1 and loc2 are booleans, not string, so the end result will be Room_true or Room_false, not Room_London or Room_NewYork.

 

But I'm still a bit confused over the checkbox thing. If there are two checkboxes, one for each city, there are four states in all: both selected, one selected, the other selected, and neither selected. It sounds like you might want to make that a drop-down choice list instead of checkboxes.

 

That having been said, I took a crack at rewriting your code to be a bit shorter, and hopefully accomplishing what you're trying to do. The caveat is that if both checkboxes are selected, it's going to stop at the first city ('London') without checking the other one. If neither checkbox is selected, it won't do anything. Here's the code:

 

function onSubmit() {
    var city = [ 'London', 'NewYork' ];
    var loc = 'Room' + (g_form.getValue('deskt') == 'perm' ? 'PM' : '') + '_';
    
    for (var i = 0; i < cities; i++) {
        if (g_form.getValue(city[i]) == 'true') {
            g_form.setValue('room', loc + city[i]);
            return;
        }
    }
    
    // If you've reached this point, no location was selected, so don't set
    // the room field value.
}

 

If the tertiary operator (?:) is confusing, all it does is evaluate the the value before the colon ('PM') if the expression to the left of the ? is true, or the value after the colon ('') if the expression to the left of the ? is false. It's just a syntactic shortcut for an if statement.

 

There are other ways to implement this, but this is flexible enough that if you add more checkboxes later, just add them to the array.

 

Hope this helps,

--Dennis R

View solution in original post

12 REPLIES 12

Brian Lancaster
Tera Sage

You only need a "+=" if you are adding to a list.  Otherwise it looks fine.

Anurag Tripathi
Mega Patron
Mega Patron

Script looks fine

 

lets say

var var_area = 'ComptuterRoom301'

 

desk ="Room" + "_" + ComptuterRoom301+'\n';   /not sure why you need \n

-Anurag

makiz63
Kilo Contributor

The script doesnt work when I add the underscore to it "-".  If I add the underscore as above on submit nothing is displayed of that variable.

 

If I remove the underscore the script works fine, it displays a value if  RoomComptuterRoom301 whereas I need one with an underscore Room_ComptuterRoom301

I have tries using other symbols like "/" and it still doesnt work so the problem might be to do with symbols in the script.

Does it make any difference if you move the underscore into the first section? 

 

i.e. desk="Room_"+'var_area';