473,395 Members | 1,629 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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.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.
Jul 23 '05 #1
6 1223
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';

Jul 23 '05 #2
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
Jul 23 '05 #3

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.

Jul 23 '05 #4
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.
Jul 23 '05 #5
JRS: In article <6t*****************@news.optus.net.au>, dated Mon, 14
Feb 2005 04:06:26, seen in news:comp.lang.javascript, RobG
<rg***@iinet.net.auau> 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.
Jul 23 '05 #6

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.

Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Krakatioison | last post by:
My sites navigation is like this: http://www.newsbackup.com/index.php?n=000000000040900000 , depending on the variable "n" (which is always a number), it will take me anywhere on the site......
5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
9
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc.,...
9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
37
by: Kevin C | last post by:
Quick Question: StringBuilder is obviously more efficient dealing with string concatenations than the old '+=' method... however, in dealing with relatively large string concatenations (ie,...
1
by: Tristan Miller | last post by:
Greetings. I am trying to write a function which toggles the display of a certain class of <div> elements in an HTML page. The CSS file initially sets some classes to "display: none", and...
1
by: siaj | last post by:
Hello all,, I want to have a page where I can have two images shown alternately in a image control. The image should keep toggling Any link or sample code will be appreciated.
1
by: Jesse Aufiero | last post by:
I have an MDI application with two maximized child forms. At any given time only one of the child forms is in the foreground and visible. To alternate between the two child forms, I can type...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.