Toggling a string

Discussion in 'Javascript' started by Dr John Stockton, Feb 13, 2005.

  1. I see, quite often, code like

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

    which is cumbersome even with the substitution T = tmp.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.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr John Stockton, Feb 13, 2005
    #1
    1. Advertising

  2. Dr John Stockton

    Matthew Lock Guest

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

    tmp.style.display = tmp.style.display == 'block' ? 'none' :
    'block';

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

    var t = tmp.style.display;
    t = t == 'block' ? 'none' : 'block';
     
    Matthew Lock, Feb 14, 2005
    #2
    1. Advertising

  3. Dr John Stockton

    RobG Guest

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


    Have a read of Mike Winter's post:

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

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

    --
    Rob
     
    RobG, Feb 14, 2005
    #3
  4. Dr John Stockton

    Matthew Lock Guest

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


    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.
     
    Matthew Lock, Feb 14, 2005
    #4
  5. Matthew Lock wrote:

    [snip]

    > Or if you didn't want to repeat "tmp.style.display" twice, this:
    >
    > var t = tmp.style.display;
    > t = t == 'block' ? 'none' : 'block';


    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.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.
     
    Michael Winter, Feb 14, 2005
    #5
  6. JRS: In article <6tVPd.532$>, dated Mon, 14
    Feb 2005 04:06:26, seen in news:comp.lang.javascript, RobG
    <> posted :
    >Matthew Lock wrote:
    >> Interesting. I would usually abbreviate the first way to this:
    >>
    >> tmp.style.display = tmp.style.display == 'block' ? 'none' :
    >> 'block';
    >>
    >> Or if you didn't want to repeat "tmp.style.display" twice, this:
    >>
    >> var t = tmp.style.display;
    >> t = t == 'block' ? 'none' : 'block';
    >>

    >
    > Have a read of Mike Winter's post:
    >
    >news://inetbws1.citec.qld.gov.au:119/
    >roups.com
    >
    > to discover why toggling between 'block' and 'none' is not
    > recommended. It's probably better to go between '' and 'none'.


    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.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr John Stockton, Feb 14, 2005
    #6
  7. Dr John Stockton

    Matthew Lock Guest

    Michael Winter wrote:
    > > var t = tmp.style.display;
    > > t = t == 'block' ? 'none' : 'block';

    >
    > 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.


    You're right. That'll learn me to post code without testing it.
     
    Matthew Lock, Feb 15, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?c2lhag==?=

    Toggling Image

    =?Utf-8?B?c2lhag==?=, May 4, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    442
    =?Utf-8?B?U2hhbmU=?=
    May 6, 2005
  2. Replies:
    1
    Views:
    395
    jasonkester
    Jul 7, 2005
  3. Replies:
    0
    Views:
    376
  4. Jamie

    Toggling display problems

    Jamie, Jan 28, 2004, in forum: HTML
    Replies:
    7
    Views:
    457
    Nik Coughin
    Jan 29, 2004
  5. kal
    Replies:
    0
    Views:
    324
Loading...

Share This Page