the problem is that the dom element 'speclist' is not created at the time the javascript code is getting executed. So I put the javascript code inside a function and called that function on body onload event.
function do_this_first(){
//appending code
}
<body onload="do_this_first()">
</body>