views:

126

answers:

3

For some reason this isn't working? I want to fill the ul#list with some items, but it's not working.

var list = "";
for (var i = 0 ; i<=history.length; i++) {
    list += "<li onclick=\"copyShortURL('"+history[i].shortURL+"');\"><div class=\"short\">"+history[i].shortURL+"</div><div class=\"long\">"+history[i].longURL+"</div></li>";
}
document.getElementById('list').innerHTML = list;

Could anyone tell me what's going wrong :( By this i mean it doesnt do anything, no list items are put into the list?

P.S. This is for mobilesafari only :)

A: 

The createElement method only takes the element name eg. document.createElement("li")

Hence you either build the DOM in code using a series of createElements and appendChilds with a smattering of assignments to innerHTML. OR you build up the HTML as a string to be appended to "list" by finally assigning to its innerHTML.

Here is the string version:-

var asHTML = [];
for (var i = 0 ; i<=history.length; i++) 
{ 
  asHTML.push("<li onclick=\"copyShortURL('"+history[i].shortURL+"');\"><div class=\"short\">"+history[i].shortURL+"</div><div class=\"long\">"+history[i].longURL+"</div></li>"); 
  document.getElementById("list").innerHTML += asHTML.join("\n");
} 
AnthonyWJones
this doesn't seem to work either?
tarnfeld
There was a spurious var in there originally, removed, tested, works on my machine dunno about Iphone
AnthonyWJones
still nothing :(
tarnfeld
Without an iphone I can't really help, as I stated on the question you might consider adding some tags to bring this question to the attention to the right people. The code posted does work on IE and Firefox on windows.
AnthonyWJones
@tamfeld, you marked this as accepted answer, and commend that it doesn't work, how does that work out?
Sander Rijken
+1  A: 

The createElement method of document takes a single string for the element name you wish to create. You could then use DOM methods and properties to assign the click handler and then use innerHTML or DOM methods to create the inner elements.

There's also an error in your loop: you need < rather <= when checking against the length of the history array.

var ele, div, list = document.getElementById("list");

var createClickHandler = function(url) {
    return function() {
        copyShortURL(url);
    };
}

for (var i = 0, len = history.length; i < len; ++i)
     {
     ele = document.createElement("li");
     ele.onclick = createClickHandler(history[i].shortURL);

     div = ele.appendChild( document.createElement("div") );
     div.className = "short";
     div.appendChild( document.createTextNode(history[i].shortURL) );

     div = ele.appendChild( document.createElement("div") );
     div.className = "long";
     div.appendChild( document.createTextNode(history[i].longURL) );

     list.appendChild(ele);
     }
Tim Down
This seems to be the most logic... should this code be above or below the element?
tarnfeld
@anthony don't worry, its a webapp - for iPhone :)
tarnfeld
@tarnfeld: You didn't think that little nugget was worth mentioning in your question? A long with exactly what mean by "doesn't work"??
AnthonyWJones
This needs to be executed once the element with id "list" is guaranteed to exist. You could put the code as it is in a `<script>` element at the end of the the body or inside a window or body onload handler.
Tim Down
hahahaha, sorryyy i will update :)
tarnfeld
A: 

I would use the DOM to append child, then add classnames and event handlers to those children.

Here is a multi browser function I use

function addEventHandler(obj,eventName,handler){
    if (document.addEventListener){
        obj.addEventListener(eventName,handler,false);
    } else if (document.attachEvent){
        obj.attachEvent("on"+eventName,handler);


    } 
}
creat an array for tyhe bew elementsa and divs;
var elements = new Array();
var newDivsShort = new Array();
var newDivsLong = new Array();

then in your for loop you can,

for (var i = 0 ; i<=history.length; i++)   
{   
 newDivsShort[i] = document.createElement('div');
 newDivsLong[i] = document.createElement('div');

 elements[i] = document.createElement('li');
newDivsShort[i].className = "short";
newDivsLong[i].className = "long";

elements[i].appendChild(newDivsShort[i]);
elements[i].appendChild(newDivsLong[i]);
addEventHandler(elements[i],click,copyShortURL(history[i].shortURL));

}

you might have to use the format similar to the timeOUt function to pass a paramater.

Alex