I am trying to add a new item to a list item. But the below code isn't adding Hyperlink to the list item I want. Can someone please advise what's wrong?
HTML:
<div>
<ul id="list1">
<li>Ut enim ad minim veniam.</li>
<li>Excepteur sint occaecat cupidatat non proident.</li>
</ul>
</div>
JavaScript:
//create new li element
var newListItem = document.createElement("li");
newListItem.textContent = "...ooo";
var ulist = document.getElementById("list1");
console.log("adding link..");
newListItem.setAttribute('href', "http://www.msn.com");
ulist.appendChild(newListItem);
console.log("added item");
you need to put the < li > tag before the entire link code and the < /li > at the end of the entire source tag.
Answer: Use the jQuery . attr() Method You can use the jQuery . attr() method to dynamically set or change the value of href attribute of a link or anchor tag. This method can also be used to get the value of any attribute.
A <link> element can occur either in the <head> or <body> element, depending on whether it has a link type that is body-ok. For example, the stylesheet link type is body-ok, and therefore <link rel="stylesheet"> is permitted in the body.
li
doesn't have the href
attribute, you have to wrap an a
tag inside li
.
var a = document.createElement("a");
var ulist = document.getElementById("list1");
var newItem = document.createElement("li");
a.textContent = "...ooo";
a.setAttribute('href', "http://www.msn.com");
newItem.appendChild(a);
ulist.appendChild(newItem);
The DEMO.
Though solved, I add some more information for you to read :)
Each element has a content model:
``[…] a description of the element's expected contents. An HTML element must have contents that match the requirements described in the element's content model.[…]''
As such the <ul>
element has a content model. Looking at the specs we find it to be:
By this we can conclude that we can not have an anchor, a
, inside the ul
element. But what about adding a href
attribute to the ul
?
Then we look at the Content attributes.
A normative list of attributes that may be specified on the element (except where otherwise disallowed), along with non-normative descriptions of those attributes. (The content to the left of the dash is normative, the content to the right of the dash is not.)
For ul
we find:
The Global attributes are the following:
In addition it can have various event handler attributes, like onmouseover
, onclick
, on...
and ARIA attributes. But, as we see, no href
attribute.
In conclusion we now know that:
ul
can not have an anchor as a child.ul
can not have the href
attribute.href
on li
element!As li
and ul
/ ol
are intertwined we first had a look at ul
. For li
we follow the same procedure: The content model for li
is:
Now, that opens up a wide range of possibilities. Here we find a
at top of the list.
And what about the attributes? For li
we find:
In other words, we now know:
ul
can not have an anchor as a child.ul
can not have the href
attribute.li
can have an anchor as a child.li
can not have the href
attribute.As pointed out by others, is to add it to an anchor that we put as a child of a li
element:
<ul>
<li><a href="myurl">Hello</a></li>
</ul>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With