Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Toggling a string

Reply
Thread Tools

Toggling a string

 
 
Dr John Stockton
Guest
Posts: n/a
 
      02-13-2005

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.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.
 
Reply With Quote
 
 
 
 
Matthew Lock
Guest
Posts: n/a
 
      02-14-2005
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';

 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      02-14-2005
Matthew Lock wrote:
> 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';
>


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
 
Reply With Quote
 
Matthew Lock
Guest
Posts: n/a
 
      02-14-2005

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.

 
Reply With Quote
 
Michael Winter
Guest
Posts: n/a
 
      02-14-2005
Matthew Lock wrote:

[snip]

> 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';


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.
 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      02-14-2005
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[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';
>>

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


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.
 
Reply With Quote
 
Matthew Lock
Guest
Posts: n/a
 
      02-15-2005

Michael Winter wrote:
> > var t = tmp[i].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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Tool for toggling wireless uhkeller@web.de Wireless Networking 1 12-07-2006 07:33 PM
New characters added when toggling Between Design & HTML Mode in Web Form Editor timburda@hotmail.com ASP .Net 1 07-07-2005 08:38 PM
New characters added when toggling Between Design & HTML Mode in Web Form Editor timburda@hotmail.com ASP .Net 0 07-07-2005 05:54 PM
Toggling Image =?Utf-8?B?c2lhag==?= ASP .Net 1 05-06-2005 10:45 AM
Toggling display problems Jamie HTML 7 01-29-2004 01:39 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57