tags:

views:

81

answers:

2

Hi,

I'm looking for the most semantically correct and sane method of marking up an ordered definition list. I have thought about hte following ways of doing it.

First I used an ordered list with 2 paragraphs within each list item. But then I remembered the definition list element.

<dl>
  <dt>Something</dt>
  <dd>Definition</dd>

  <dt>Something2</dt>
  <dd>Definition2</dd>
</dl>

However as I need to be able to access each definition programmatically through Javascript I'm not sure that this would work.

The solution to this might be to separate the definitions into different dl tags. But that doesn't look very good at all. Is there a more correct way? Or am I incorrect in assuming that is more complicated to access the "normal" definition list through Javascript?

<dl>
  <dt>Something</dt>
  <dd>Definition</dd>
</dl>
<dl>
  <dt>Something2</dt>
  <dd>Definition2</dd>
</dl>
A: 

Use names and IDs.

<dl id="1">

Document.getElementById("1");

https://developer.mozilla.org/En/DOM/Document.getElementById

oneat
A nitpick: IDs cannot start with a number.
cpharmston
Are you sure ??
oneat
+1  A: 

However as I need to be able to access each definition programmatically through Javascript I'm not sure that this would work.

Just get each definition term <dt> and then get the element next to it in the DOM tree, which should always be the definition description <dd>. jQuery is of great help in here. Here's an SSCCE, just copy'n'paste'n'run it:

<!doctype html>
<html lang="en">
    <head>
        <title>SO question 2172138</title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"&gt;&lt;/script&gt;
        <script>
            $(document).ready(function() {
                $('#dl dt').each(function(i, dt) {
                    dt = $(dt);
                    dds = dt.nextAll().map(function() { return $(this).is('dd') ? this : false; });
                    alert(dt.text() + " = " + dds.text());
                });
            });
        </script>
    </head>
    <body>
        <dl id="dl">
            <dt>Term1</dt><dd>Desc1a</dd><dd>Desc1b</dd>
            <dt>Term2</dt><dd>Desc2a</dd>
            <dt>Term3</dt><dd>Desc3a</dd><dd>Desc3b</dd>
        </dl>
    </body>
</html>
BalusC
This doesn't take into account the perfectly valid use case of having multiple definitions per term.
cpharmston
Lol, what an attitiude. Fixed, thanks ;)
BalusC