Re: Setting several styles within one javascript function



On 29/09/2005 16:35, Dylan Parry wrote:

Using a pointed stick and pebbles, Spartanicus scraped:

function panel() {
  document.getElementById('panel').style.display='hidden';

The value, hidden, doesn't apply to the display property, so this is a CSS issue, not one of scripting. So, which is it: display/none, or visibility/hidden?


A well-written script will be robust, and test its environment before using any features. In this instance, though, it might be better to prepare beforehand:

  /* Establish if the getElementById method is implemented by the
   * host. If not, create a replacement so that the method can be
   * called successfully, even if the result will never be viable.
   *
   * Support for older, obscure object models can be added here.
   * See the clj FAQ notes with regard to 'IDed Element Retrieval'.
   */
  if(!document.getElementById) {
    (document.getElementById = function() {
      return null;
    }).notViable = true;
  }

  function setStyle(element, property, value) {
    (element.style || element)[property] = value;
  }

  function panel() {
    var content, panel;

    if((panel = document.getElementById('panel'))
      && (content = document.getElementById('content')))
    {
      setStyle(panel, 'display', 'none');
      setStyle(content, 'marginLeft', '30px');
    }
  }

This is not necessarily the best alternative. It may depend on what the rest of the code does.

[snip]

I had problems with a similar function a while back. IIRC the problem
was caused by having the /wrong kind of quotes/ and changing them to "s
made all the difference. I might be wrong, of course ;)

Probably. There is no difference between single and double quotes in ECMAScript.


Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
.