Toggling a string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dr John Stockton

    Toggling a string


    I see, quite often, code like

    if (tmp[i].style.display == 'none')
    tmp[i].style.display = 'block';
    else
    if (tmp[i].style.display == 'block')
    tmp[i].style.display = 'none';
    or
    if (tmp[i].style.display == 'none')
    tmp[i].style.display = 'block';
    else
    tmp[i].style.display = 'none';

    which is cumbersome even with the substitution T = tmp[i].style .

    Consider

    X = {'none':'block' , 'block':'none' /* , 'undefined' : 'none' */ }

    T = F.X0 ; T.value = X[T.value]

    where of course X is set once and for all. When the second line is
    executed, F.X0.value is toggled. If the X-line comment symbols are
    removed, false values are corrected after TWO goes.

    In practice, X should be spelt blockORnone or similar.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
    <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
  • Matthew Lock

    #2
    Re: Toggling a string

    Interesting. I would usually abbreviate the first way to this:

    tmp[i].style.display = tmp[i].style.display == 'block' ? 'none' :
    'block';

    Or if you didn't want to repeat "tmp[i].style.display" twice, this:

    var t = tmp[i].style.display;
    t = t == 'block' ? 'none' : 'block';

    Comment

    • RobG

      #3
      Re: Toggling a string

      Matthew Lock wrote:[color=blue]
      > Interesting. I would usually abbreviate the first way to this:
      >
      > tmp[i].style.display = tmp[i].style.display == 'block' ? 'none' :
      > 'block';
      >
      > Or if you didn't want to repeat "tmp[i].style.display" twice, this:
      >
      > var t = tmp[i].style.display;
      > t = t == 'block' ? 'none' : 'block';
      >[/color]

      Have a read of Mike Winter's post:

      news://inetbws1.citec.qld.gov.au:119...oglegroups.com

      to discover why toggling between 'block' and 'none' is not
      recommended. It's probably better to go between '' and 'none'.

      --
      Rob

      Comment

      • Matthew Lock

        #4
        Re: Toggling a string


        RobG wrote:[color=blue]
        > to discover why toggling between 'block' and 'none' is not
        > recommended. It's probably better to go between '' and 'none'.[/color]

        Yeah I am aware that it requires the toggled element to already have
        display: none or display: block as part of its style. Good point.

        Comment

        • Michael Winter

          #5
          Re: Toggling a string

          Matthew Lock wrote:

          [snip]
          [color=blue]
          > Or if you didn't want to repeat "tmp[i].style.display" twice, this:
          >
          > var t = tmp[i].style.display;
          > t = t == 'block' ? 'none' : 'block';[/color]

          However, that would fail. You would assign a string value to t, not a
          reference to the display property. Assigning to t a second time would
          update that variable, nothing more.

          What you could do is store a reference to the style object:

          var s = tmp[i].style;
          s.display = ('' == s.display) ? 'none' : '';

          OR

          var dP = {'' : 'none', 'none' : ''};

          s.display = dP[s.display];

          A very interesting idea, John.

          Mike

          --
          Michael Winter
          Replace ".invalid" with ".uk" to reply by e-mail.

          Comment

          • Dr John Stockton

            #6
            Re: Toggling a string

            JRS: In article <6tVPd.532$Ud4. 41794@news.optu s.net.au>, dated Mon, 14
            Feb 2005 04:06:26, seen in news:comp.lang. javascript, RobG
            <rgqld@iinet.ne t.auau> posted :[color=blue]
            >Matthew Lock wrote:[color=green]
            >> Interesting. I would usually abbreviate the first way to this:
            >>
            >> tmp[i].style.display = tmp[i].style.display == 'block' ? 'none' :
            >> 'block';
            >>
            >> Or if you didn't want to repeat "tmp[i].style.display" twice, this:
            >>
            >> var t = tmp[i].style.display;
            >> t = t == 'block' ? 'none' : 'block';
            >>[/color]
            >
            > Have a read of Mike Winter's post:
            >
            >news://inetbws1.citec.qld.gov.au:119...000cwo.googleg
            >roups.com
            >
            > to discover why toggling between 'block' and 'none' is not
            > recommended. It's probably better to go between '' and 'none'.[/color]

            But that does not just toggle between those two; it converts anything
            but 'block' to 'block' and 'block' to 'none'.



            ==

            When you find a relevant article on the Web, it's best to include its
            essence, as well as its URL, in News; some of us read news off-line.
            Copy'n'paste should be able to extract something suitable from a well-
            written item.

            --
            © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
            <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
            <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
            <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

            Comment

            • Matthew Lock

              #7
              Re: Toggling a string


              Michael Winter wrote:[color=blue][color=green]
              > > var t = tmp[i].style.display;
              > > t = t == 'block' ? 'none' : 'block';[/color]
              >
              > However, that would fail. You would assign a string value to t, not a[/color]
              [color=blue]
              > reference to the display property. Assigning to t a second time would[/color]
              [color=blue]
              > update that variable, nothing more.[/color]

              You're right. That'll learn me to post code without testing it.

              Comment

              Working...