views:

50

answers:

1

Searching for a simple first-child detection via javascript (no framework).

It should add class "first" for the first child of the element. Like a css-hacks for ie, but in javascript and for a html file.

Must work similar to :first-child

When I say no framework, I mean some code like this:

<script type="text/javascript">document.documentElement.id = "js"</script>

For example:

<div class="terms">
    <dl>
        <dt>Term 1 name</dt>
        <dd>Term 1 description</dd>
    </dl>
    <dl>
        <dt>Term 2 name</dt>
        <dd>Term 2 description</dd>
    </dl>
    <dl>
        <dt>Term 3 name</dt>
        <dd>Term 3 description</dd>
    </dl>
</div>

Three <dl>, the first one includes Term 1.

This css won't work in IE6, thats why I'm searching for a javascript solution. CSS-hacks are not allowed.

.terms dl:first-child { float: right; }
+2  A: 

This will do what you want but you really do need to add an id to the div to facilitate finding it.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
    <title></title>
    <style type="text/css">
        .terms { }
        .terms dl.first { float: right; }
    </style>
</head>
<body>
    <div id="terms" class="terms">
        <dl>
            <dt>Term 1 name</dt>
            <dd>
                Term 1 description</dd>
        </dl>
        <dl>
            <dt>Term 2 name</dt>
            <dd>
                Term 2 description</dd>
        </dl>
        <dl>
            <dt>Term 3 name</dt>
            <dd>
                Term 3 description</dd>
        </dl>
    </div>
</body>

<script type="text/javascript">

    function firstChild(element) {
        for (var i = 0; i < element.childNodes.length; i++) {
            var child = element.childNodes[i];
            if (child.nodeType == 1) {
                return child;
            }
        }
    }
    var terms = document.getElementById("terms");

    firstChild(terms).className += " first";

</script>

</html>

Reference:

nodeType Constants

ELEMENT_NODE : 1 ATTRIBUTE_NODE : 2 TEXT_NODE : 3 CDATA_SECTION_NODE : 4 ENTITY_REFERENCE_NODE : 5 ENTITY_NODE : 6 PROCESSING_INSTRUCTION_NODE : 7 COMMENT_NODE : 8 DOCUMENT_NODE : 9 DOCUMENT_TYPE_NODE : 10 DOCUMENT_FRAGMENT_NODE : 11 NOTATION_NODE : 12
Sky Sanders
Thanks Sky Sanders. Is there any way to make it shorter?
Happy
@glister - not that I can see. actually, if anything, this snippet should probably be a bit longer, including guards that the element passed in is, in fact, a dom element etc. in my experience, shorter is highly overrated. clarity of purpose is to be cherished in any code.
Sky Sanders
ok, I will try to use it a bit later and accept if works. Thanks again.
Happy
Does it add a class for a first child element?
Happy
I would recommend learning just a bit about JavaScript first. Do not blindly use snippets, understand them beforehand. Otherwise, you're doomed.
Fyodor Soikin
@glister - what fyodor says is correct. While I am happy to provide guidance, e.g. how to get the first child of an element, if your level of understanding of javascript is such that you cannot tell if it adds a classname you really do need to learn a bit more about js. In this case, googling how to add a classname to an element would be a good start. Cheers...
Sky Sanders
@glister - updated the answer to accomplish your goals.
Sky Sanders