Maurice
Maurice

Reputation: 1157

How to put more hyperlinks in the <li> part in my javascript generated list

I have a javascript creating a list. All works, see my fiddle: http://jsfiddle.net/mauricederegt/mjdyW/4/

Now this script produces:

<div id="container">
  <ul>
    <li>
      <a href="/best-known-for">Link1</a>
    </li>
  </ul>
  <ul>
    <li>
      <a href="/life-events">Link1</a>
    </li>
  </ul>
</div>

As you can see it has just 1 hyperlink in the <li> part. I want to add more hyperlinks in the <li> part so the script produce the list like:

<div id="container">
  <ul>
    <li>
      <a href="/best-known-for">Link1</a>
      <a href="/best-antoher">Some oher link</a>
      <a href="/best-antoher2">Another oher link</a>
    </li>
  </ul>
  <ul>
    <li>
      <a href="/life-events">Link1</a>
      <a href="/life-events2">Link2</a>
    </li>
  </ul>
</div>

How to do edit my javascript so it will read/create more hyperlinks from the script??

In addition, I also want to add more <li> in a single <ul>, kinda stuck in this one too. My end goal has to look somewhat like this:

<div id="container">
  <ul>
    <li>
      <a href="/best-known-for">Link1</a>
      <a href="/best-antoher">Some oher link</a>
      <a href="/best-antoher2">Another oher link</a>
    </li>
    <li>
      <a href="/best-known-for2">Link2</a>
      <a href="/best-antoher2">Some oher link2</a>
      <a href="/best-antoher22">Another oher link2</a>
    </li>
  </ul>
  <ul>
    <li>
      <a href="/life-events">Link1</a>
      <a href="/life-events2">Link2</a>
    </li>
  </ul>
</div>

Kind regards

Upvotes: 1

Views: 170

Answers (4)

adeneo
adeneo

Reputation: 318302

var Menu = function(data) {
    this.data = data;
};

Menu.prototype.render = function() {
    //console.log(this.data)
    var ul = $("<ul />");
    $.each(this.data, function(i, e) {
        var li = $("<li />");
        $.each(e, function(j, f) {
            li.append($("<a></a>", {
                href: f.url,
                text: f.title
            }));
        });
        ul.append(li);
    });
    return ul;
};

Menu.renderMenus = function(menus, root) {
    $.each(menus, function(key, val) {
        var m = new Menu(val),
            ul = m.render();
        ul.appendTo(root);
    });
}

var menu = 
[
    [//first ul
        [ //first li 
            { //first <a> element
                title: "First UL, First li, First a",
                menuCaption: "Best Known For Caption",
                url: "/best-known-for"
            },
            { //second <a> element
                title: "First UL, First li, Second a",
                menuCaption: "Best Known For Caption",
                url: "/best-known-for"
            }
        ],
        [ //second li 
            { //only <a> element
                title: "First UL, Second li, First a (not first-child, and not red)",
                menuCaption: "Choose a life Event",
                url: "/life-events"
            }
        ]
    ],
    [//second ul
        [ //first li in second ul
            { //only <a> element
                title: "Second UL, First li, First a",
                menuCaption: "Hello Kitty",
                url: "/google"
            },
            { //second <a> element
                title: "Second UL, First li, Second a",
                menuCaption: "Best Known For Caption",
                url: "/best-known-for"
            }
        ]
    ] //you can add as many <li> or <a> elements you wish by following the 
      //pattern in this object literal
];


Menu.renderMenus(menu, $("#container"));​

FIDDLE

Upvotes: 3

andrei.r
andrei.r

Reputation: 11

i modified the code a little to create the structure you want. check the comments.

var Menu = function(data) {
       this.data = (data.length) ? data : [data]; 
    };

    Menu.prototype.render = function(root) {
        // generate one li per menu item and one anchor for each element from data
        var li = $("<li></li>");
        $.each(this.data, function(link) { 
            $("<a></a>", {
                href: link.url,
                text: link.title
            }).appendTo(li);
        });
        li.appendTo($('#menuContainer'));
    };

    Menu.renderMenus = function(menus, root) {
        // create global ul here and add and id so you can point later to it
        var ul = $("<ul></ul>").attr('id','menuContainer');
        ul.appendTo(root);
        $.each(menus, function(key, val) {
            var m = new Menu(val);
            m.render(root);
        });
    }


    // modified the structure a little. you other arrays for anchors
    var menu = [
        [{
            title: "Link1",
            menuCaption: "Best Known For Caption",
            url: "/best-known-for",

        },{
            title: "Link2",
            menuCaption: "Best Kno2wn For Caption",
            url: "/best-known-for2",

        }
           ],

            {
            title: "Link1",
            menuCaption: "Choose a life Event",
            url: "/life-events",
        }


    ];

    Menu.renderMenus(menu, $("#container"));
})

Upvotes: 1

John Leehey
John Leehey

Reputation: 22240

You can append them separately (this will also work if you incorporate it into a loop):

Menu.prototype.render = function(root) {
    var ul = $("<ul></ul>");
    var li = $("<li></li>");
    li.append($("<a></a>", {
        href: this.data.url,
        text: this.data.title
    })).appendTo(ul);

    // More links can be added into the li
    li.append($("<a></a>", {
        href: this.data.url,
        text: this.data.title
    }));

    ul.appendTo(root);
};

You can also add li items into a ul the same way:

ul.append(li.clone());

Upvotes: 1

Kpower
Kpower

Reputation: 1257

<div id="container">
  <ul>
    <li>
      <a href="/best-known-for">Link1</a>
      <a href="/best-antoher">Some oher link</a>
      <a href="/best-antoher2">Another oher link</a>
    </li>
    <li>
      <a href="/best-known-for2">Link2</a>
      <a href="/best-antoher2">Some oher link2</a>
      <a href="/best-antoher22">Another oher link2</a>
    </li>
    <li>
      <a href="/life-events">Link1</a>
      <a href="/life-events2">Link2</a>
    </li>
  </ul>
</div> 

Will that work?

Upvotes: -4

Related Questions