html - How to add content into a dynamically created div in JavaScript? -
i have code shows 'top of page' link when scroll down:
window.addevent('load', function() { new jcaption('img.caption'); }); function fade_me(num){ var smoothtop=document.id('smoothtop'); if(smoothtop){smoothtop.fade(window.getscrolltop()<250?0:num);} } window.addevent('domready',function(){ var scroll=new fx.scroll(window,{ 'duration': 500, 'transition': fx.transitions.bounce.easeinout, 'wait': false }); var smoothtop=new element('div',{ 'id': 'smoothtop', 'class': 'smoothtop', 'style': 'position:fixed; display:block; visibility:visible; zoom:1; opacity:0; cursor:pointer; right:5px; bottom:5px;', 'title': '', 'html': '', 'events':{ mouseover: function(){fade_me(1);}, mouseout: function(){fade_me(0.7);}, click: function(){scroll.totop();} } }).inject(document.body); document.id('smoothtop').setstyle('opacity','0'); }); window.addevent('scroll',function(){fade_me(0.7);}); //this added var ii = document.getelementbyid('smoothtop'); ii.childnodes[0].nodevalue = '<i class="icon icon-chevron-up"></i>'; //these 2 lines
as can see code generates div id smoothtop. has arrow-up picture indicate top of page. instead, want use bootstrap's glyphicon using
<i class="icon icon-chevron-up"></i>
i have tried add content div smoothtop. when inspect code firebug, says:
typeerror: ii null var ii = document.getelementbyid('smoothtop');
i couldn't figure and/or doing wrong? want ask how can add <i></i>
div has been created js?
given example, easiest way use html
property when create element:
var smoothtop=new element('div',{ 'id': 'smoothtop', 'class': 'smoothtop', 'style': 'position:fixed; display:block; visibility:visible; zoom:1; opacity:0; cursor:pointer; right:5px; bottom:5px;', 'title': '', 'html': '<i class="icon icon-chevron-up"></i>', // <-- right here 'events':{ mouseover: function(){fade_me(1);}, mouseout: function(){fade_me(0.7);}, click: function(){scroll.totop();} } }).inject(document.body);
Comments
Post a Comment