[Javascript] Fw: hide other divs

Laurent Muchacho elmuchacho at gmail.com
Wed Apr 12 04:16:40 CDT 2006


HI Michael,

You cool try this

var divElementsId = ''
if(divElementsId = divElements[i].getAttribute("id")){
if(divElementsId.substring(0,6) == mySubstring && divElementsId != myDiv)
  {
   divElements[i].style.display = "none";
        }
    }
}

Mainly you will be first checking for the existance of the attribute before
using it, also declaring your variable is alway good practice.
But in your case you should not require to use getAttribute method as you
are requesting an id and if you do divElements[i].id will then either return
the id or an empty string.

The reason why you don't need getAttribute is simple the id attribute is an
"built in" attribute that mean that all browser will recognise
mydiv.attribute if you create a custom attribute like eg: <div order="1"> at
this point you need getAttribute to pick it up in all browser simply because
some browser won't recognise mydiv.order as an attribute.

I hope this help, happy programming

Laurent


Laurent

On 4/12/06, Michael Borchers <list at tridemail.de> wrote:
>
> >>>
> the following function works fine in IE, not in FF.
> I guess i worked a little bit dirty and FF is missing some kind of ID.
>
> example div:
> <div name="ordersDiv" id="ordersDiv" style="display:block">
>
> function:
> function hideOtherDivs(myDiv, mySubstring)
> {
>  var divElements = document.getElementsByTagName("div");
>
>     for(i=0;i<divElements.length;i++)
>  {
>         divElementsId = divElements[i].getAttribute("id");
>
>   if(divElementsId.substring(0,6) == mySubstring && divElementsId !=
> myDiv)
>   {
>    divElements[i].style.display = "none";
>         }
>     }
> }
>
> call:
> javascript:hideOtherDivs('ordersDiv', 'orders');
> <<<
>
> ok, my suggestion wasn't that bad.
> since i have a huge menu in my site there are lots of divs.
> some of them have no properties, not name nor id.
> IE doesn't care about this in the for(), but firefox
> suddenly stops and breaks off.
>
> i could create an own attribute i guess, but isn't there
> some way to read out all div tags that only have ids
> starting with a string like 'orders' ?!
>
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.evolt.org/pipermail/javascript/attachments/20060412/57ae11dc/attachment.htm>


More information about the Javascript mailing list