SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Oct 2007
    Location
    Bilthoven, Netherlands
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Angry shorthand for testing more values of same property in if statement?

    A
    In my current example, I have an array and want to disable the elements in the array - but only if they have a property type that equals either 'radio' or 'checkbox'. Looping through the array, this works:
    #1
    Code:
    var t = this.inputs[i].type || null; 
    if(t==='checkbox' || t==='radio') { this.inputs[i].disabled=true; }
    But it annoys me in this sort of cases that I have to type the variable's name twice. I tried this instead which didn't work:
    #2
    Code:
    t==='checkbox' || 'radio'
    And this which worked in Firefox and Chrome, but not in IE8:
    #3
    Code:
    t==='checkbox' | 'radio'
    Surely, there must be an shorter way of doing it than #1 above?

    ===================================================================

    B
    While I'm at it, I also find the if-else shorthand great:
    #4
    Code:
    (value) ? a=1 : a=2;
    But it annoys me that it is not possible to do this as in a true shorthand for if without else:
    #5
    Code:
    (value) ? a=1; 
    // or at least:
    (value) ? a=1 : ;
    It works with a hack, e.g. like this:
    #6
    Code:
    (value) ? a=1 : 0 ;
    That's fine although it looks ugly in the code. Can anyone tell me, why this is not possible then:
    #7
    Code:
    (value==='mystring') ? a=1 : 0 ;

  2. #2
    SitePoint Zealot
    Join Date
    Oct 2007
    Location
    Bilthoven, Netherlands
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is where it gets odd. Example #2 above does not work entirely as intended. But elsewhere I have a similar case and it works like a charm:
    #8
    Code:
      var keydown = function(event) {
        if (event.keyCode === 17 || 16) { // CTRL=17, SHIFT=16
          _this.toolState = _this.TOOLSTATE.TOGGLE;
        }
      };
      $(document).bind('keydown', keydown);
    It responds perfectly to both the CTRL key and the SHIFT key.

    The odd thing about #2 is that there was a object in that array which was not an HTMLElement and had no .type property at all [it was an Array(String)] - but it still "passed the test" of the if(t==='checkbox' || 'radio') and went into the following code block {}.

    Anyone who can shed some light on this mystery?

  3. #3
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if (event.keyCode === 17 || 16)

    well, this is always true, because whatever event keycode is, 16 is a truthy value.

    If you want to save typing, use the result of the test as the value of disabled:


    var who=this.inputs[i] || '', t=who.type;
    who.disabled= t=='checkbox' || t=='radio;

    // It does this: who.disabled=( t=='checkbox' || t==='radio')? true: false;
    Last edited by mrhoo; Jan 13, 2012 at 14:14. Reason: sense

  4. #4
    SitePoint Zealot
    Join Date
    Oct 2007
    Location
    Bilthoven, Netherlands
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ha ha - thanks! I forgot to test with any other keys than just ctrl and shift! It proves that no matter which key I press, it has the same effect - obviously, as you say, the number 16 always evaluates to true! Silly me .. :|

    And thanks for your other suggestion with " who.disabled= t=='checkbox' || t=='radio; " !


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •