From your Javascript you have a few issues.
First off
var ul = d.getElementsByTagName('ul');
This returns an array of ul
elements therefore you cannot call var li = ul.getElementsByTagName('li')
directly on the array.
You will need to iterate each ul
in the array and get the li
's from the array item such as.
var ul = d.getElementsByTagName('ul');
for (var u = 0; u < ul.length; u++) {
var li = ul[u].getElementsByTagName('li')
}
Now just as the ul
variable the li
is also an array so you will need to iterate each li
item in the array.
var ul = d.getElementsByTagName('ul');
for (var u = 0; u < ul.length; u++) {
var li = ul[u].getElementsByTagName('li')
for (var i = 0; i < li.length; i++) {
}
}
The next problem is you dont create a class
element. class
is an attribute of the li
element. This can be accessed via the className
property on each element.
To make this work you can put it all together as.
var ul = d.getElementsByTagName('ul');
for (var u = 0; u < ul.length; u++) {
var li = ul[u].getElementsByTagName('li')
for (var i = 0; i < li.length; i++) {
li[i].className = 'Bebida';
}
}
Here is a JSFiddle for your review.
I must add one more point. The first loop of capturing the ul
elements is irrelavant. You could skip this directly and capture all the li
elements. This will reduce the amount of loops required.
Given that you are capturing all the ul
elements and then capturing all the li
for that ul
you can simply get all the li
elements from the document. This of course is only the case if you only want the li
that are children of ul
s and not other elements (such as ol
).
This can be reduced to.
var liArray = d.getElementsByTagName('li');
for(var i=0; i<liArray.length;i++)
liArray[i].className = 'Bebida';