HTML approach
Since you want to display a title for a read-only (e.g. Enabled=false
) DropDownList I believe that there is no reason to use any JavaScript at all and stick to plain HTML.
See the following example:
<div title="<%=DropDownList2.SelectedItem.Text %>">
<asp:DropDownList ID="DropDownList2" runat="server" Enabled="false">
<asp:ListItem Value="1" Text="Disabled item 1"></asp:ListItem>
<asp:ListItem Value="2" Text="Disabled item 2"></asp:ListItem>
<asp:ListItem Value="3" Text="Disabled item 3"></asp:ListItem>
</asp:DropDownList>
</div>
I tested it in all browsers (Firefox 32, IE 11, Chrome 37, Opera 24) and it works with no issues.
JavaScript approach
If, on the other hand, you need a JavaScript / jQuery approach you could use the following example. In the mouseover
event on the div the DropDownList is temporary enabled to get its value and then disable it again. Once the value is retrieved, the title
attribute of the div
is changed.
It is important that the div has some padding so that the cursor will hover over the div and eventually the event will be triggered.
HTML:
<div class="dynamictoolip" title="title">
<asp:DropDownList ID="DropDownList3" runat="server" Enabled="false">
<asp:ListItem Value="1" Text="Disabled item 1"></asp:ListItem>
<asp:ListItem Value="2" Text="Disabled item 2"></asp:ListItem>
<asp:ListItem Value="3" Text="Disabled item 3"></asp:ListItem>
</asp:DropDownList>
</div>
CSS:
.dynamictoolip {
display:inline-block;
padding:4px;
}
JavaScript:
jQuery('.dynamictoolip').mouseover(function () {
var jThis = jQuery(this);
var jDdl = jThis.find('select');
var value = jDdl.find("option:selected").text();
if (jDdl.prop('disabled')) {
jDdl.removeAttr('disabled');
jDdl.attr('disabled', 'disabled');
jThis.attr('title', value);
}
else {
jThis.attr('title', value);
}
});
Note: One issue I noticed here is that if the user moves the cursor too fast the mouseover
event is not triggered. I also tried it with the mouseenter
and mouseout
events (and also tried plain JavaScript and no jQuery) but it didn't make any difference.
Edit based on latest comments
Although your <asp:DropDownList>
will be in both disabled and enabled state, the tooltip should always remain on the parent element.
The only thing we need to do is when the <asp:DropDownList>
changes its value, the title
attribute of the parent element to change also, that can be accomplished with a bit of JavaScript.
In the following code snipped I added a simple link to enable the <asp:DropDownList>
. Also, here is a jsfiddle with the code.
HTML
<div>
simple ddl 2:
<span title="<%=DropDownList2.SelectedItem.Text %>">
<asp:DropDownList ID="DropDownList2" runat="server" CssClass="ddl2" Enabled="false">
<asp:ListItem Value="1" Text="Disabled item 1"></asp:ListItem>
<asp:ListItem Value="2" Text="Disabled item 2"></asp:ListItem>
<asp:ListItem Value="3" Text="Disabled item 3"></asp:ListItem>
</asp:DropDownList>
</span>
<a href="#" class="edit_ddl2">edit</a>
</div>
JavaScript
jQuery('.edit_ddl2').click(function () {
jQuery('.ddl2').prop('disabled', false);
return false;
});
jQuery('.ddl2').change(function () {
var jThis = jQuery(this);
jThis.parent().attr('title', jThis.find("option:selected").text());
jThis.prop('disabled', true); // remove this line if you want the element to stay enabled
});