3

The following code for onmouseover for the div, not showing tooltip when I first move mouse over the div element, but if I click somewhere and bring the mouse and it shows tooltip. Not sure i'm doing anything incorrectly? is there proper way to show tooltip for READ ONLY dropdownlist inside the div?

enter image description here

DropDown.ascx

<div style="z-index:99;position:relative;padding:1px;" onmouseover="this.title=<%= ddl.ClientID %>.options[<%= ddl.ClientID %>.selectedIndex].text">
    <asp:DropDownList ID="ddl" runat="server" CssClass="etms-dropdown-width" style="position:relative;z-index:-1;">
    </asp:DropDownList>
</div>

DropDown.ascx.cs

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           ....                
          this.ddl.Attributes["onmouseover"] = "this.title=this.options[this.selectedIndex].text";
          foreach (ListItem item in this.ddl.Items)
          {
             item.Attributes["title"] = item.Text;
          }
          this.ddl.DataBind();
        }

        else
        {
            this.ddl.Attributes["onmouseover"] = "this.title=this.options[this.selectedIndex].text";
            foreach (ListItem item in this.ddl.Items)
            {
                item.Attributes["title"] = item.Text;
            }
        }
    }
kneethan
  • 423
  • 1
  • 8
  • 22
  • I thought its simple question for asp.net experts! – kneethan Oct 03 '14 at 13:35
  • http://stackoverflow.com/questions/15872488/tooltip-for-drop-down-list-items – tdbeckett Oct 06 '14 at 18:33
  • Are you expecting to show tooltop on hovering over `Priority Type`?, i have noticed that DIV is used to wrap only DDL , not the label. – Arindam Nayak Oct 06 '14 at 19:12
  • Yes that's correct the tool tip is not for label since its fully visible, but the drop down list item sometimes hidden due to the length, so tooltip is the way to see the contents of it. so If I hoover "Affected Employee(.. " then it should show the tooltip with full title" – kneethan Oct 07 '14 at 04:00
  • The browser I'm testing this in Firefox. – kneethan Oct 14 '14 at 05:37

6 Answers6

4

The problem is that you use negative z-index in your dropdown list. I test everything and is working if you remove negative z-index.

Negative z-indexes disable mouse interaction. Using z-indexes greater than or equal to 0 enables mouse interaction.

source

Community
  • 1
  • 1
Alex Char
  • 32,879
  • 9
  • 49
  • 70
  • But it says `z-index:99;`, it is 99, not negative! – Arindam Nayak Oct 06 '14 at 18:52
  • `z-index` 99 is in parent element. The child `select` has `z-index: -1`. – Alex Char Oct 06 '14 at 18:53
  • Got it, but since OP need to show tooltip for a `disabled` or `readonly` control, that does not allow any mouse interaction. Even i have tried original code posted by OP, it is working fine for me, but there can be issue with tooltip for disabled control. – Arindam Nayak Oct 06 '14 at 19:26
  • You can't set `select` `readonly`. You can set thought `disabled` but again it doesn't work. – Alex Char Oct 06 '14 at 19:31
  • Yes, i agree on `disabled` thing, but select with disabled works, http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_select_disabled – Arindam Nayak Oct 06 '14 at 19:33
  • @AlexChar I tried your solution and its behaviour no different than what I already have. the problem for me is, when I very FIRST hoover the mouse over disabled dropdownlist nothing happens, but if I click somewhere and then hoover over ddl then it shows. in your solution same for when I bring the first time no tooltip on disabled ddl, but when I takeout the mouse pointer and hoover again then it shows tooltip. why its not showing the tooltip very first time? I'm using firefox browser. – kneethan Oct 14 '14 at 06:14
  • @AlexChar Still its not showing tooltip for the first hoover of mouse. still your solution bit better than what I have. I'm rewarding the points to you. in the meantime if you know how to resolve that first time hoover not showing tooltip (my browser is firefox) then pls advise me here. – kneethan Oct 14 '14 at 06:19
3

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
});
Tasos K.
  • 7,979
  • 7
  • 39
  • 63
  • Thanks for your help, What I really wanted is to show tooltip wrapped by div while the dorpdownlist is disabled. and there is edit button on that page and when clicked the edit button , dropdownlist becomes enabled and it should show tooltip as normal. my current code works to show tooltip while disabled but it takes time and I have mouseover a few times, but when enabled it shows very quickly and consistent. I want that quick and consistent when its disabled. – kneethan Dec 03 '14 at 06:25
  • I see, so you are still looking for a solution? – Tasos K. Dec 03 '14 at 10:30
  • Yes I'm still looking for solution that shows tooltip while dropdown is disabled as well as when dropdown is enabled (when edit button is clicked). My current code shows tooltip on both , but while ddl is disabled, it shows tooltip after multiple mouse over on dropdownlist. – kneethan Dec 04 '14 at 14:05
  • I added a third code snippet. Hopefully this will be ok :) – Tasos K. Dec 04 '14 at 19:25
  • I'm trying this, I'm very new to jQuery and stuck on here, is showing as tooltip 'ddl.options[ddl.selectedIndex].title]' – kneethan Dec 04 '14 at 21:14
  • Putting JavaScript inside arguments will not execute, is there a reason why you are not using ``? – Tasos K. Dec 04 '14 at 23:54
  • title="<%=DropDownList2.SelectedValue %>" shows me just number not that corresponding text. – kneethan Dec 05 '14 at 22:49
  • You are right, sorry about that! The correct code for that is ``. I updated my code in all examples, both the .aspx and the JavaScript code – Tasos K. Dec 06 '14 at 00:00
  • 1
    you the hero, it works now as I wanted. Thanks a lot for your patient and explanation with clear simple working examples. – kneethan Dec 09 '14 at 08:25
0

Since you need to show tooltip for disabled control, that might not work for some browsers. You need to control that using JS. SOURCE- Tooltip is not displayed for a disabled html button in mozilla firefox

You can add simple javascript to set title/tooltip on document load.

<body onload="SetAttr()">

Assuming parent DIV has id = dvDDL, like - <div style="z-index:99; id='dvDDL' .. />

<script> function SetAttr() { document.getElementById('dvDDL').setAttribute('title','<%= ddl.ClientID %>.options[<%= ddl.ClientID %>.selectedIndex].text'); } </script>

Community
  • 1
  • 1
Arindam Nayak
  • 7,346
  • 4
  • 32
  • 48
  • I tried didn't work.In my given code I get tooltip sometimes when I clicked outside and bring the mouse pointer hoover over disabled ddl, but in your solution nothing happened. Just to make sure I got your approach ,
    – kneethan Oct 14 '14 at 06:16
0

You dont need the javascript mouseover stuff. The following is enough to have a tooltip appear when a user hovers over a div:

<div title="this is a tooltip"></div>  

So, you need to initially set this value on page load. To do this, give the div an id and set runat=server so that you can access its attributes on the server:

<div runat="server" id="tooltip"></div>

Then in your code behind, when the page loads, set the title attribute:

this.tooltip.Attributes["title"] = this.ddl.SelectedValue  

If you are using:

AutoPostBack=true

You are done. But if you are not using AutoPostBack and you want the tooltip to change dynamically when the user changes the selected value, you need a little javascript, but put it on the onChange event of your dropdown list. That way you know the dropdown will always be enabled when it is called (otherwise how would they have changed anything). so again in your code behind, you could do something like this:

ddl.Attributes["onChange"] = "document.getElementById('" + div.ClientID "').title=this.options[this.selectedIndex].text";     
Keith
  • 718
  • 8
  • 12
  • I tried and didn't work while the dropdownlist was on disabled, didn't check in edit mode. I will debug to see what is missing. – kneethan Oct 09 '14 at 16:22
  • during debug, inside the pageload, this.ddl.SelectedValue shows as empty string – kneethan Oct 09 '14 at 18:31
0

A simple solution using JavaScript would be to do this..

$(element).bind("mouseover", function () {

    if ($(element)[0])
    {
        $(element)[0].title = $(element)[0].options[$(element)[0].selectedIndex].text;
    }       

});
0

Try just using JQuery, javascript has a hard time dealing with dynamic content, or multiple browser support.

Change:

this.title = this.options[this.selectedIndex].text

To:

$(this).attr('title', $(this).find('option:selected').text());
user3334871
  • 1,251
  • 2
  • 14
  • 36
  • I tired but no difference than what I have. My problem is only in disabled dropdownlist and hoover mouse pointer near the dropdownlist it takes many hoover attempts to show tooltip. however when the dropdownlist is enabled via edit button on that page, the tooltip appear as quickly as I move mouse pointer over the dropdownList. – kneethan Dec 02 '14 at 07:14