views:

62

answers:

1

Part One: I'm trying to figure out how to use the DOM and Javascript to create an array containing the links in multiple lists. The problem is I need each UL to have a unique array containing the links; the only unique ID I am able to use in this case is the name value in the link tags. I will not be able to add anything else to the markup. The javascript reference will need to be contained in a single script, with one reference to the script at the bottom of the page.

Part Two: What I ultimately need to do, is to hide each of the lists, and replace them with just the first two links, along with a "view all" link below the two links that, when clicked, adds the other two links to the list.

Again, I can't add any markup, divs, etc. etc.; it must be completely based on the information below, the DOM and javascript.

Thanks for any help y'all can provide!

<ul>
    <li>
        <a href="#" name="obj_a">Section One, Article One</a>
    </li>
    <li>
        <a href="#" name="obj_b">Section One, Article Two</a>
    </li>
    <li>
        <a href="#" name="obj_c">Section One, Article Three</a>
    </li>
    <li>
        <a href="#" name="obj_d">Section One, Article Four</a>
    </li>
</ul>

<ul>
    <li>
        <a href="#" name="obj_e">Section Two, Article One</a>
    </li>
    <li>
        <a href="#" name="obj_f">Section Two, Article Two</a>
    </li>
    <li>
        <a href="#" name="obj_g">Section Two, Article Three</a>
    </li>
    <li>
        <a href="#" name="obj_h">Section Two, Article Four</a>
    </li>
</ul>
+2  A: 

I am using jQuery for my solutions ;)

Part One:

var list = new Array();
$.each($('ul'), function(index, value) {
  list.push(new Array());
  $.each($(value).find('li a'), function(index2, value2){
    list[list.length - 1].push(value2.href);
  });
});

Part Two:

I really don't understand the requirements, but have a look at jQuery it really makes those takes easy.

Tobias P.
It sounds like a Javascript library isn't an option, due to the requirement of "the javascript reference will need to be contained in a single script." Is this the case, OP?
Stuart Branham
You can add a JS-Library in the script, where's the problem, you don't need to put jQuery in another file.
Tobias P.
Negatory; I do have access to the yahoo YUI library but not jQuery in this case.I think Marcel's idea above might work splendidly though.
Michael
The basic algorithm is still the same. It's just more verbose using all the standard functions rather than jQuery's convenience utilities.
Chuck
Also, with jQuery, the above can be more concisely written as something like `$("ul:has(li>a)").map(function (item) { return $(item).find("a").filter(function (index) { return index < 2; }) })`. As it is, you're reinventing the `map` and `filter` functions.
Chuck