-1

I am trying to get my google maps to display for several locations but the onclick as shown below is not getting to my function b. What am i 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>

1 Answers1

0

Without seeing your entire situation, event delegation could solve your problem. Based on the minimal info I have, I got your 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, this means you'd need to have some sort of pre-existing element on the page. I added a container div, but it could be (and probably should be) the body tag itself.

If this is a large app, this solution could help with memory leaks as well.

If your app/site needs to support older IE versions, look at something like this in addition to addEventListener.

Is it imperative that you have the init function? Does it do more than what you have listed? If not, I'd recommend ditching it since it isn't necessary in solving this specific problem. It seems like window.onload is unnecessary in your situation unless (like I said) that function does other things not listed.

UPDATE Here is more info on how to use logic in event delegation... assuming you do end up attaching it to the body tag, you'll need to filter the clicks.

Community
  • 1
  • 1
Nick Johnson
  • 190
  • 1
  • 12