Search This Blog

Showing posts with label jQuery. Show all posts
Showing posts with label jQuery. Show all posts

Sunday, 22 September 2019

Add ToolTip to DropDownList Items in ASP.Net Using jQuery


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script>
        $(document).ready(function () {
            //Using for loop
            //$("select").click(function () {
            //    var s = this;
            //    for (i = 0; i < s.length; i++)
            //        s.options[i].title = s.options[i].text;
            //    if (s.selectedIndex > -1)
            //        s.onmousemove = function () { s.title = s.options[s.selectedIndex].text; };
            //});

            //OR

            //Using jquery each loop
            $("select").click(function () {
                var s = this;
                $("select option").each(function () {
                    $(this).attr("title", $(this).val());
                });
                if (s.selectedIndex > -1)
                    s.onmousemove = function () { s.title = s.options[s.selectedIndex].text; };
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:DropDownList ID="DropDownList1" runat="server">
                <asp:ListItem>Select State</asp:ListItem>
                <asp:ListItem>Andhra Pradesh</asp:ListItem>
                <asp:ListItem>Arunachal Pradesh</asp:ListItem>
                <asp:ListItem>Assam</asp:ListItem>
                <asp:ListItem>Bihar</asp:ListItem>
                <asp:ListItem>Chattisgarh </asp:ListItem>
                <asp:ListItem>Goa</asp:ListItem>
                <asp:ListItem>Gujarat</asp:ListItem>
                <asp:ListItem>Haryana</asp:ListItem>
                <asp:ListItem>Himachal Pradesh </asp:ListItem>
                <asp:ListItem>Jammu and Kashmir</asp:ListItem>
                <asp:ListItem>Jharkhand</asp:ListItem>
                <asp:ListItem>Karnataka</asp:ListItem>
                <asp:ListItem>Kerala </asp:ListItem>
                <asp:ListItem>Madhya Pradesh</asp:ListItem>
                <asp:ListItem>Maharashtra </asp:ListItem>
                <asp:ListItem>Manipur</asp:ListItem>
                <asp:ListItem>Meghalaya</asp:ListItem>
                <asp:ListItem>Mizoram</asp:ListItem>
                <asp:ListItem>Nagaland </asp:ListItem>
                <asp:ListItem>Odisha </asp:ListItem>
                <asp:ListItem>Punjab </asp:ListItem>
                <asp:ListItem>Rajasthan</asp:ListItem>
                <asp:ListItem>Sikkim </asp:ListItem>
                <asp:ListItem>Tamil Nadu  </asp:ListItem>
                <asp:ListItem>Telengana</asp:ListItem>
                <asp:ListItem>Tripura</asp:ListItem>
                <asp:ListItem>Uttar Pradesh </asp:ListItem>
            </asp:DropDownList>
        </div>
    </form>
</body>
</html>

Output


Alternatively You can use the following code in code behind page to show tooltip in Asp.net DropDownList Control.

Note:- Use this code after where DropDownList bind completed.

for (int i = 0; i < DropDownList1.Items.Count; i++)
{
      DropDownList1.Items[i].Attributes.Add("title", DropDownList1.Items[i].Text);
}

Wednesday, 26 June 2019

How to disable non selected options in dropdown list using jQuery?


When you click on the button, except selected item from dropdown list other items disabled.

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script>
        $(document).ready(function () {
            $("#Button1").click(function (e) {
                e.preventDefault();
                //This line disable the DropDown
                //$('#DropDownList1').prop('disabled', true);

                //This line disable the unselected item from DropDown List
                $('#DropDownList1 option').each(function () {
                    !this.selected && !$(this).index() == 0 ? $(this).attr('disabled', true) : "";
                });
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:DropDownList ID="DropDownList1" runat="server">
                <asp:ListItem>Select</asp:ListItem>
                <asp:ListItem>1</asp:ListItem>
                <asp:ListItem>2</asp:ListItem>
                <asp:ListItem>3</asp:ListItem>
                <asp:ListItem>4</asp:ListItem>
            </asp:DropDownList>
            <br />
            <asp:Button ID="Button1" runat="server" Text="Button" />
        </div>
    </form>
</body>
</html>