Toggling a string

D

Dr John Stockton

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

Matthew Lock

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

RobG

Matthew said:
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/[email protected]

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

Matthew Lock

RobG said:
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.
 
M

Michael Winter

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
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Mon, 14
Feb 2005 04:06:26, seen in RobG
Matthew said:
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/[email protected]
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top