I'm attempting to add a class for highlighting certain elements on my web page. I'm using the jQuery("selector").addClass("class")
function to do that. However, it does not work.
function toggleHighlight(selector, on) {
if (on) {
$(selector).addClass("highlighted");
} else {
$(selector).removeClass("highlighted");
}
}
When the on
argument is true
and the selector
argument is a valid selector, it enters the if
clause and exits. Unfortunately the classes of the elements the selector refers to, remain unchanged. I seem to missing something fairly fundamental. Help would be appreciated.
EDIT:
I left out the console.log($(selector));
and console.log($(".highlighted"));
statements earlier, for clarity. The first returns the right elements, the second, none (called after .addClass()
). The elements themselves are <path>
elements created by leaflet for a map (from GeoJSON). As such, the HTML is rather hard to reproduce.
I have called the function form a button on the page: <input type="button" id="toggle-0702" class="btn btn-default" onClick="toggleHighlight('.node-0112', true)" value="turn on" \>
as well as directly from the JS console. Same results.
EDIT 2:
Apparently this is an issue with either <path>
elements, or (more likely) leaflet. To demonstrate this: http://jsfiddle.net/mrz40jgw/. Notice the shy
class on the second circle.