show/hide javascript

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dylanb

    show/hide javascript

    Hi can anyone tell me how to alter this script so it will works on
    classes and id's;
    <script>
    function showhide(id){
    if (document.getEl ementById){
    obj = document.getEle mentById(id);
    if (obj.style.disp lay == "none"){
    obj.style.displ ay = "";
    } else {
    obj.style.displ ay = "none";
    }
    }
    }
    </script>

    its called by;

    <a href="javascrip t:void(0)" onmouseover="sh owhide('divname ')">
    link</a>

    <div style="display: none;" id="divname">
    some content
    </div>

    I just need to make it work on div classes
  • Thomas 'PointedEars' Lahn

    #2
    Re: show/hide javascript

    dylanb wrote:
    Hi can anyone tell me how to alter this script so it will works on
    classes and id's;
    <script>
    <script type="text/javascript">
    function showhide(id){
    if (document.getEl ementById){
    obj = document.getEle mentById(id);
    if (obj.style.disp lay == "none"){
    obj.style.displ ay = "";
    } else {
    obj.style.displ ay = "none";
    }
    }
    }
    A bit more indentation would make this script legible, but not good.
    For example, `obj' was not declared and so is not local; it should be

    var obj = ...

    instead.
    </script>
    >
    its called by;
    >
    <a href="javascrip t:void(0)" onmouseover="sh owhide('divname ')">
    link</a>
    Should be at least

    <script type="text/javascript">
    document.write( '<a href="javascrip t:void(0)" onclick="return false"'
    + ' onmouseover="sh owhide(\'divnam e\')">link<\/a>');
    </script>

    so that no-script users are not bothered with a non-working element.
    <div style="display: none;" id="divname">
    some content
    </div>
    It is a really bad idea to hide content first. This way it will stay
    inaccessible when there is insufficient script and DOM support available.
    I just need to make it work on div classes
    Since unlike an ID a CSS class may be used for several elements in a
    document, the solution is a not a trivial one. See e.g.
    dhtml.getElemBy ClassName() in http://PointedEars.de/scripts/dhtml.js

    Another way is to apply the XPath expression `//*[@class="foo"]' on the BODY
    node or a decendant node, see <http://developer.mozil la.org/en/docs/XPath>.
    (This will be implemented in the next version of dhtml.js.)


    PointedEars
    --
    realism: HTML 4.01 Strict
    evangelism: XHTML 1.0 Strict
    madness: XHTML 1.1 as application/xhtml+xml
    -- Bjoern Hoehrmann

    Comment

    Working...