var a; titleText=""; hardCodedTitle="";
var menuCount = document.getElementsByClassName().length;
$('a.UserSelectedItems').each(function(){
for(a=0; a<menuCount; a++){
title += $('a.UserSelectedItems')[a].innerText;
if(title == "B-1"){
hardCodedTitle = "B-1: Business sector 1";
$('a.UserSelectedItems').attr('title',hardCodedTitle);
}
else if(title == "B-2"){
hardCodedTitle = "B-2: Business sector 2";
$('a.UserSelectedItems').attr('title',hardCodedTitle);
}
else if(title == "D-5"){
hardCodedTitle = "D-5: Development group 5";
$('a.UserSelectedItems').attr('title',hardCodedTitle);
}
else{
hardCodedTitle = "Not categorized";
$('UserSelectedItems').attr('title',hardCodedTitle);
}
}
});
<div class="ui menu" id="Menu">
<a class="UserSelectedItems" title="A-1">A-1</a>
<a class="UserSelectedItems" title="A-2">A-2</a>
<a class="UserSelectedItems" title="B-1">B-1</a>
<a class="UserSelectedItems" title="B-2">B-2</a>
<a class="UserSelectedItems" title="D-2">D-2</a>
<a class="UserSelectedItems" title="D-5">D-5</a>
<a class="UserSelectedItems" title="E-2">E-2</a>
</div>
<input type="text" id="populateTitle" placeholder="hardcoded title populate OnClick menu items">
//OnClick of menu item, hardcode title value as per the js should be populated in this text box
I have a requirement where on hovering the each menu item(menu is dynamically updated as per user selection), hard coded title has to be populated dynamically. I'm able to get the dynamic menu item as title as of now, and I need help to populate hard-coded title based on each dynamic menu item using javascript.
Above is example code(not the exact code that works) to show my scenario. Firstly based on user selection, menu items are populated(few might be unselected). So my requirement is to display hardcoded value as title based on menu items selected by user.
If user selects items: B-1,B-2,D-5 , then hardcoded title has to be shown as pop-up on hovering each menu item.
div class="ui menu" id="Menu">
...
<a class="UserSelectedItems" title="${eachDataSet.getKey()}">${eachDataSet.getKey()}</a>
</div>
My actual html code looks somewhat as above. Thanks!
2nd edit... I used the below code(answered by @rucha) and it's working fine. Now additional requirement is I have an input field of type text. How can I populate title(hard-coded title for each menu item) on click of each item, as text box value?