Asp.Net Dropdownlist tooltip or title with JS

Asked

Viewed 189 times

0

I’m starting in Javascript, and would like my DropDownList of a control .ascx, when selecting an item, display its full value when you hover over it, as if it were a tooltip or title. This is my Dropdown:

<asp:DropDownList CssClass="dropdown" ID="ddlDescricao 
 OnSelectedIndexChanged="ddlDescricao_SelectedIndexChanged"
                        runat="server" TabIndex="0">   

I tried to do it that way:

    <script type="text/javascript">
    function addToolTip() {
        var ddl = document.getElementById('<%= ddlDescricao.ClientID %>');
        for (var i = 0; i < ddl.options.length; i++) {
            ddl.options[i].title = ddl.options[i].text;
        }
    }
</script>

But it’s not working.

I’m using jQuery 1.7

From Now Thank You All.

--Edit 11/02/2016 08:27

I’m calling the function from the dropdown onmouseover.

2 answers

0


Just use the following instruction:

$("select[name*='ddlDescricao']")
    .prop('title', $("select[name*='ddlDescricao'] option:selected").text());

The selector was made using the contains attribute because the controls name is usually modified in the rendering.

Update: Adding the code of the second objection

var $dropdown = $("select[name*='ddlDescricao']");
$dropdown.prop('title', $dropdown.attr('title') + ': ' + $dropdown.children("option:selected").text());
  • Good afternoon, thanks at first it worked, however, when there is more than one dropdown on the page, it gives an "append" in the values of Tittle, for example the value of dropdown1 is carlos, the value of dropdown2 is twenty, in dropdown2 the "title" will be like carlosvinte, instead of just "twenty".

  • I changed the answer

  • Good morning Felipe, thanks for the reply, but unfortunately it happens practically the same, see in the image of the edition attached.

0

I don’t know if I understand the problem very well, but if you need to change the title of an element using javascript you can do it this way:

<script>
   function alteraTitulo() {
      var ddl = document.getElementById('ddlTeste');
      var title = ddl.options[ddl.selectedIndex].text;
       ddl.title = title;
    }
 </script>

<select id="ddlTeste" onchange='alteraTitulo()'>
<option>Valor 1</option>
<option>Valor 2</option>
<option>Valor 3</option>
</select>

In this way I select the element in question in var ddl = document.getElementById('ddlTeste');, get the text of the selected option var title = ddl.options[ddl.selectedIndex].text; and change the title ddl.title = title;.

Jsfiddle

Editing

A simple solution (using javascript only) for several selects would you pass the element id to your function. It would be more or less that:

<script>
   function alteraTitulo(id) {
      var ddl = document.getElementById(id);
      var title = ddl.options[ddl.selectedIndex].text;
       ddl.title = title;
    }
 </script>

<select id="ddlTeste" onchange='alteraTitulo("ddlTeste")'>
<option>Valor 1</option>
<option>Valor 2</option>
<option>Valor 3</option>
</select>

<select id="ddlTeste1" onchange='alteraTitulo("ddlTeste1")'>
<option>Teste 1</option>
<option>Teste 2</option>
<option>Teste 3</option>
</select>

Jsfiddle

Using jQuery you can perform the exchange when running the event .change(), thus making the title replacement. It would look like your code:

$('select').change(function(){
var ddl = $(this);
var title = ddl.find("option:selected").text();
ddl.prop('title', title); 
console.log(title);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<select>
<option>Valor 1</option>
<option>Valor 2</option>
<option>Valor 3</option>
</select>

<select>
<option>Teste 1</option>
<option>Teste 2</option>
<option>Teste 3</option>
</select>

Jsfiddle

  • Good morning, thanks for the reply, but this way when there is more than one dropdown on screen, the value selected in a dropdown, will be established for all others.

  • I thought of a way to do this, but I don’t know how to implement it, that’s the code to get a sense more or less of what I tried to do: function alteraTitulo() {&#xA; var $dropdown = $("select[name*='ddlDescricao']"); for (i = 0; i < $dropdown.children.length; i++) { $dropdown[i]. prop('title', $dropdown[i].find("option:Selected").text(); } }

  • @Carloshenrique Are you only using javascript or jquery? But is that exactly what you want? Of course, working for more and one dropdown

  • I am using jQuery and javascript, anyone who proposes me the solution. What I want is that when I pass the mouse over a value selected from a dropdown, it shows me what is written in the selected value, because there are cases where the dropdown has space for 20 characters and the value has 40 for example, so the user would not know what is selecting, however, the solutions I tried work if there is only a dropdown on screen, when there is more than 1 dropdown, it gets lost, ends up assigning the value of the previous dropdown or next, I will try to implement its last edition.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.