views:

80

answers:

6

HI all

I want to do something like

    if(something.val() == 'string1')
{
    something.val('string2');
}
    else if(something.val() == 'string2')
    {
        something.val('string1')
    }

But in one line of code. I can't quite remember how it's done, but it involves question marks and colons lol

Sorry to be vague

James

+4  A: 

Try:

something.val(something.val() == 'string1' ? 'string2' : 'string1');

It is called a ternary expression.

Matthew Manela
Exactly what I was looking for - thanks!
JamWaffles
But this will set `string1` even if value is `string3`, not only for `string2`
dev-null-dweller
@dev-null-dweller this isn't a problem - I'm toggle the text in a button between 'Reply' and 'Cancel' :)
JamWaffles
A: 
 something.val( something.val() == 'string1'? 'string2' : 'string1' );

or for clarification

val astring = something.val() == 'string1'? 'string2' : 'string1';
something.val( astring );
drachenstern
+1  A: 

You mean to use the ternary operator:

something.val((something.val() == 'string1') ? 'string2' : 'string1');
Mike Axiak
why are you grabbing the jquery object back out?
drachenstern
+4  A: 

Look ma, no ternary operator!

The following works because Javascript short circuits boolean expressions.

If something == string1 then evaluate string2 -- since string2 is a truthy value and the next expression involves the OR operation there is no need to continue. Stop and return string2.

If something !== string1 then it will skip the next operand because if it is false, there is no point in evaluating the next operand (with AND). It will "jump" to the OR operation and return string1.

function toggleString(something, string1, string2) {
   return something == string1 && string2 || string1;
}

something.val(toggleString(something.val(), "string1", "string2"));

If you want the assignment done:

function toggleValue(something, string1, string2) {
   something.val(something.val() == string1 && string2 || string1);
}

toggleValue(something, "string1", "string2"); // something is a jQuery collection

In the end however, I would end up using the ternary operator because this solution might be unclear to other programmers. If you come from Java or other languages, you may expect the function to return a boolean because of all the boolean operators.

CD Sanchez
Ok, so you still have to assign to `something` in there somewhere. You didn't assign, you only compared.
drachenstern
@Daniel ... yeah, I know that. My point was more like the ternary reads faster than that, and you still have to show the assignment in your clever post ;)
drachenstern
+1 clever solution, though you should explain *why* this works.
Daniel Vandersluis
Thanks - a bit long winded for what I want as I'm doing this inline, but handy nonetheless!
JamWaffles
@drachenstern: Since everybody else posted the ternary operator solution there really is no point in posting the same thing. This function will toggle between the two values based on the something. How he uses it (if at all) will be up to him -- it's would be up to him to modify it if he wants to make the function easier to use. Besides, `something` is meant to be a string (which are immutable) so I wouldn't be able to change it anyways. I'll add the explanation and add the version that does the assignment to satisfy *you people*.
CD Sanchez
lol@you people ... I added +1 for the clever. I just wondered if it was too clever as a two line post (ignoring brackets) ... and as for the "since everybody else posted the ternary" ~ it was what he asked for, and I was the second person to actually reply, but yeah, it was quite an avalanche of the same answer repeatedly.
drachenstern
See my answer towards the bottom if you're interested in making this a jquery function so you can call $('input').toggleVal('str2','str2');
fehays
A: 
  something.val(something.val() == 'string1' ? 'string2' : 'string1');
James Curran
+1  A: 

How about using @Daniel's code along with a jquery function:

$.fn.toggleVal = function (str1, str2) {
     return this.val(this.val() == str1 && str2 || str1);
};
$("input").toggleVal('string 1', 'string 2');
fehays
There really is no benefit for using my solution over a ternary operator - I just thought it would be nice to offer an alternate version :). Nice though! isn't `this` already a jQuery object? So there is no need to wrap it with the `$` function. Also, jQuery probably returns `this` when calling `val` as a setter, so you might be able to merge those two lines into a single one line return statement.
CD Sanchez
Indeed you are correct. Thanks for the tip :) I will update the code. And I like yours over the ternary operator because question marks scare me :P
fehays