javascript - Get element by id not working -
i trying google maps display several locations onclick shown below not getting function b. doing wrong? thanks
javascript
function b(){ dlat=40.856771; dlng=0.578294; alert(dlat); getloc(); } function init() { document.getelementbyid("click").onclick = b; } window.onload = init
html
<ul data-role="listview" data-split-icon="star" data-split-theme="a"> <li><a href="#1"><h1>task1</h1></a><a href="#popup" id="click" data-rel="popup" data-position-to="window" data-transition="fade"> </a></li> </ul> <div data-role="popup" id="popup" data-overlay-theme="a" data-theme="a" data-corners="false" data-tolerance="15,15"> <a href="#" data-rel="back" data-role="button" data-theme="a" data-icon="delete">close</a> <iframe src="map.html" width="320" height="320" seamless></iframe> </div>
without seeing entire situation, event delegation solve problem. based on minimal info have, got function call working using event delegation: http://jsfiddle.net/uufs3/1/
document.getelementbyid("container").addeventlistener("click", b, false); function b(){ dlat=40.856771; dlng=0.578294; alert(dlat); getloc(); }
of course, means you'd need have sort of pre-existing element on page. added container div, (and should be) body tag itself.
if large app, solution memory leaks well.
if app/site needs support older ie versions, @ like this in addition addeventlistener.
is imperative have init function? more have listed? if not, i'd recommend ditching since isn't necessary in solving specific problem. seems window.onload unnecessary in situation unless (like said) function other things not listed.
update here more info on how use logic in event delegation... assuming end attaching body tag, you'll need filter clicks.
Comments
Post a Comment