Javascript button OnClick Event

Hi,

I have a for loop that adds a button to a google maps infowindow. What I'm trying to do it use the iterator as the ID of the button so the OnClick event loads a certain url from an array of urls.

The page is never loaded though as the neighborhoodGraphics object is always undefined. Is there a better way to do this?

function addMarker() {
        var contentString = '<div id="content">'+
            '<div id="siteNotice">'+
            '</div>'+
            '<h1 id="firstHeading" class="firstHeading">'+neighborhoodNames[iterator]+'</h1>'+
            '<div id="bodyContent">'+
            '<p>Testing</p>'+
            '<button class="btn btn-primary btn-large" id='+iterator+' onclick="loadGraphic(this.ID)">To Graphic &raquo;</button>'+
            '</div>'+
            '</div>';

        var infowindow = new google.maps.InfoWindow({
            content: contentString
        });
        var marker = new google.maps.Marker({
            position: neighborhoods[iterator],
            map: map,
            draggable: false,
            animation: google.maps.Animation.DROP,
            ID: iterator
        });
        markers.push(marker);
        google.maps.event.addListener(marker, 'mouseover', function() {
            
            infowindow.open(map,marker);
        });
       
        iterator++;
    }
function loadGraphic(i)
{
    alert(i);
    window.location.href = neighborhoodGraphics[i];


}

Open in new window

LVL 1
wint100Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tr0gd0rCommented:
On line 8 you are trying to access "this.ID" instead of "this.id".
'<button class="btn btn-primary btn-large" id='+iterator+' onclick="loadGraphic(this.ID)">'+

Open in new window

And you could simplify by using iterator directly instead of putting it into the id attribute and pulling it out later:
'<button class="btn btn-primary btn-large" onclick="loadGraphic('+iterator+')">'+

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.