3

What is the <li/> tag for..?

Can anyone point/find/share some documentation related to it..?

I found the code in this answer by "thecodeparadox"

I have tried hard to find some documentation related to that in vain..

Community
  • 1
  • 1
Rony
  • 88
  • 1
  • 7
  • I just find the related (possibly duplicate) question [here](http://stackoverflow.com/questions/867916/creating-a-div-element-in-jquery) which might help you a lot. – Bhojendra Rauniyar Sep 10 '14 at 05:34

3 Answers3

4

jQuery provides an alternative to the traditional DOM createElement.

$("<li/>"); //create li element

If you wish to modify the element or bind events to it, you can do like below:

$("<li/>", { 
  click: function(){}, //allows you to bind events
  id: "test", // can be set html attribute
  addClass: "clickable" //allows you to bind jquery methods 
});

Documentation can be found here.

Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
  • 1
    Adding on to this, in the example @Rony provides, a link `` is being appended to the list item (which itself has content added to it) – Jason Sep 10 '14 at 05:22
  • Are there any online docs that you can refer me to..? I am new to JQuery, and books on these topics tend to get outdated fast .. with newer, leaner and better ways to do the same thing... – Rony Sep 10 '14 at 05:27
3

HTML provides tags for constructing ordered <ol> ("numbered") and unordered <ul> ("bulleted") lists.

Each item in such a list is indicated by the <li> tag.

<ul>
  <li>First!</li>
  <li>Second Base</li>
  <li>Three Strikes and you are Out</li>
</ul>

(sadly, often you will see the closing tag </li> omitted) The notation <TAG/> is a shorthand for an empty tag (that is, equivalent to <TAG></TAG>).

There is little use for an empty list item, besides as a placeholder to be expanded upon later by javascript.

John Hascall
  • 9,176
  • 6
  • 48
  • 72
1

The <li/> tag in jQuery is for tag creation, It's shorthand for <li></li>, in jQuery documentation:

When the parameter has a single tag (with optional closing tag or quick-closing) — $( "<img />" ) or $( "<img>" ), $( "<a></a>" ) or $( "<a>" ) — jQuery creates the element using the native JavaScript .createElement() function.

A basic sample:

$('<li/>').text('Hello World 1').appendTo('ul');
$('<li></li>').text('Hello World 2').appendTo('ul');
$('<li>').text('Hello World 3').appendTo('ul');

See this jsFiddle Demo.

dashtinejad
  • 6,193
  • 4
  • 28
  • 44