459,453 Members | 961 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,453 IT Pros & Developers. It's quick & easy.

# Toggling a string

 P: n/a 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 © JL/RC: FAQ of news:comp.lang.javascript jscr maths, dates, sources. TP/BP/Delphi/jscr/&c, FAQ items, links. Jul 23 '05 #1
6 Replies

 P: n/a 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

 P: n/a 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

 P: n/a 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

 P: n/a 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

 P: n/a JRS: In article <6t*****************@news.optus.net.au>, 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.googlegroups.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 © JL/RC: FAQ of news:comp.lang.javascript jscr maths, dates, sources. TP/BP/Delphi/jscr/&c, FAQ items, links. Jul 23 '05 #6

 P: n/a 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 discussion thread is closed

Replies have been disabled for this discussion.