I have a dynamically generated HTML element that includes a title attribute which appears in a tooltip when hovering, like:
<div title="tooltip text" class="classname" id="identifier"></div>
I would like to change the contents of the title attribute using javascript code set to run as part of the HTML render. Currently, my code is as follows:
var changeTooltip = function(){
var TooltipElement = document.getElementsByClassName("classname");
if (TooltipElement.title = "tooltip text"){
TooltipElement.title = "new message";
};
};
window.onload = changeTooltip();
This code leaves the original string in the div's title attribute once the page renders fully. Can anyone explain why, and possibly show the correct way? Note that I must use JS, I cannot use JQuery.