jQuery on click is raised by text selection

So today I was building some basic toggle capabilities into a web page. Everything is working well, then I test selecting the text. Of course this causes the element to collapse on mouse up.

After some googling Stackoverflow of course had the answer…

$('h4').on('click', function() {
    if (getSelection() == "") {
        //do stuff
    }
});

All it took was wrapping my code in a single if statement using getSelection().

Advertisements