Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #18041

Re: Simple question about div show or hide

From Cezary Tomczyk <cezary.tomczyk@gmail.com>
Newsgroups comp.lang.javascript
Subject Re: Simple question about div show or hide
Date 2013-01-09 22:44 +0100
Organization Aioe.org NNTP Server
Message-ID <kckoce$v6$2@speranza.aioe.org> (permalink)
References <b7e6883d-fe02-41ca-b4e8-89cfaccde7f6@googlegroups.com>

Show all headers | View raw


W dniu 2013-01-09 21:54, justaguy pisze:
> Hi,
>
> Problem with the following code for simple Div show or hide: Second
> click is not possible.  Tested against Firefox 17 and Chrome 23 (both
> on Windows 7).
>
> What's wrong?  Am I missing something obvious?
>
> Please note, I'm using double ## here instead of a single # because
> the server side scripting language, ColdFusion or Railo that I'm
> using, consider # as a special symbol, so, I'm using double ## to
> escape to one.
>
> <div><a href="##sh" onclick="if
> (document.getElementById('t').style.display='none'){document.getElementById('t').style.display='inline';}else{alert('2nd
> test click
> called');document.getElementById('t').style.display='none';}">test
> div show/hide</a></div>
>
> <div id="t" style="display:none">test data</div>

I would write in that way*:

(note: needed to remove <> due to problem with post of this message)

[HTML]

a href="#somewhere" id="mylink" style="display:none" Test /a

[JavaScript]

var switchView, elm,
     b = true;

elm = document.getElementById('mylink');

switchView = function(e){
     if (b) {
         elm.style.display = 'block';
     } else {
         elm.style.display = 'none';
     }
     b = !b;

     if (e.preventDefault) {
         e.preventDefault();
     }
     if (window.event) {
         window.event.returnValue = false;
     }
     return false;
};

if (window.addEventListener) {
     el.addEventListener('click', switchView, false);
} else {
     el.attachEvent('onclick', switchView);
}

* 1) assuming that code is loaded when DOM is ready
   2) this is very simple example and probably could be improved in a 
many ways
   3) it's better to use 'class' and switch classes instead of inline styles

-- 
Cezary Tomczyk
http://www.ctomczyk.pl/

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Simple question about div show or hide justaguy <lichunshen84@gmail.com> - 2013-01-09 12:54 -0800
  Re: Simple question about div show or hide Scott Sauyet <scott.sauyet@gmail.com> - 2013-01-09 13:29 -0800
  Re: Simple question about div show or hide Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-01-09 22:44 +0100
    Re: Simple question about div show or hide justaguy <lichunshen84@gmail.com> - 2013-01-09 14:25 -0800
      Re: Simple question about div show or hide Richard Yates <richard@yatesguitar.com> - 2013-01-09 14:47 -0800
    Re: Simple question about div show or hide Martin Leese <please@see.Web.for.e-mail.INVALID> - 2013-01-10 09:52 -0700
      Re: Simple question about div show or hide Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-01-10 21:02 +0100
  Re: Simple question about div show or hide Hans-Georg Michna <hans-georgNoEmailPlease@michna.com> - 2013-01-10 18:26 +0100
    Re: Simple question about div show or hide "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-01-10 19:37 +0100
  Re: Simple question about div show or hide Danny <dann90038@gmail.com> - 2013-01-10 12:47 -0800

csiph-web