Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Bram Huisman
7,387 Pointson click check hasClass --> if hasClass --> get attr name and display it
Hi guys,
I am making a map of Holland with some markers on it. What is want to achieve is when someone clicks on a marker.. the name attribute of that marker needs to get printed out!..
Here is jsfiddle.. link
every circle element has the class marker.. when the user clicks on a marker, a for loop loops through the array. This array is filled with regions of Holland.. those regions are also classes as you can see on every circle element. When the outcome matches with the class name it needs to get the name attribute of that element and print it out within the title div.
What am i doing wrong?
1 Answer
elk6
22,916 PointsHi Bram,
Slightly different approach. Since you already assigned the class names to the different red dots, why not use them to make your JQuery a bit simpler.
Like this:
$('.marker').click(function(){
$('#title').text($(this).attr('class').split(' ')[1]);
});
No more need for the array. The code just outputs the second class name to the #title div. As long as the name of your "provincie" is always the second class it will get printed to the title div. :)
Elian
Bram Huisman
7,387 PointsBram Huisman
7,387 PointsAh.. thank you man! Sometimes i think to myself, why can't i think of that..
elk6
22,916 Pointselk6
22,916 PointsNo problem. We've all been there.
Good luck from a fellow Dutchie. :)