I want my javascript function to convert an array from my HTML document into an HTML list in a separate document. However, I am encountering problems with this and think it might be because the array isn't being recognised.
My HTML document is a digital edition in which each word is enclosed in an <a> element, which has attributes. One of these attributes (@abbr) has been compiled by my XSLT transformation to resemble a javascript array; i.e.
<a id="123" onclick="myFunction(this.id)" abbr="['x', 'y', 'z']">word</a>
As part of my javascript function (triggered by clicking on the word), I want to present x, y, and z as rows in an unordered list. This is what I have at the moment:
function myFunction(id) {
var el = document.getElementById(id);
var abbrArray = el.getAttribute('abbr');
var abbrList = '<ul></ul>';
var abbrListItems = abbrArray.join('</li><li>');
var abbrListFull = abbrList.innerHTML = '<li>' + abbrListItems + '</li>';
}
This is what the abbrListFull variable should stand for:
<ul>
<li>x</li>
<li>y</li>
<li>z</li>
</ul>
The function goes on to create a table and place the abbrListFull variable as the inner HTML of one of the cells.
However, the function fails because of "Uncaught TypeError: abbrArray.join is not a function". Can anyone explain what is causing this problem and how to fix it?
I realise that this error is sometimes due to a function being performed on the wrong type of object. Alerting abbrArray produces "['x', 'y', 'z']", which looks like an array, but I am wondering if there is something else I need to do to make this recognizable as an array within the function.
Thanks!