0

I have an asp.net textbox:

    <asp:TextBox runat="server" id="tb" ToolTip="A" />.

That I need to be able to set the tooltip based on a value selected from a dropdown list.

    If (var == "something") {  
        ToolTip = "B";  
    } else {  
        ToolTip = "A";  
    }

I'm setting the ToolTip to an inital value based on what the DropDown defaults to. When it changes I need to set the ToolTip.

I have seen and tried several solutions from the web, but have been unable to make them work.

I'm using and VS 2008 and IE7, the site is running using the VS server and not IIs. I have verified that I have the correct field and CAN change the 'title' using the debugger.

Thank You in advance

4
  • Which .net version are you on? Commented May 17, 2011 at 18:49
  • VS 2008 Framework 3.5, sorry should have mentioned this before. Commented May 18, 2011 at 13:39
  • Are you trying to do this on client-side or server-side? Is your DDL set to autopostback? Commented May 20, 2011 at 17:10
  • EVERYTHING is client-side, the person who created the site wanted little if any work being done on the server. Where this code will reside, is in the client-side function invoked when the DDL changes. Commented May 20, 2011 at 18:29

3 Answers 3

2

I tried this like this:

<asp:TextBox runat="server" ID='txtSomething' ToolTip='Some tooltip' CssClass='myTextBox'></asp:TextBox>

And in my jQuery I wrote:

$(function () {

        var maybe = true;

        if (maybe) {
            $('.myTextBox').attr('title', 'Some other tooltip');
        }

    });

And my text box when rendered shows 'Some other tooltip' as its title

Sign up to request clarification or add additional context in comments.

5 Comments

I've tried this and no luck, I even tried forcing it to change all the time. I noticed the ClientIDMode tag, is that something new in 2010? I'm on 2008 and do not have it, also does it affect the attr call?
Still no luck, the '#' notation works in the rest of the code. I've also tried 'document.getElementById' with no luck. I've also tried 'tooltip' in place of title, '.tooltip', '.option'.
It's so strange...do a firebug check on what you get on your page. check if the class name is being applied properly.
I looked at it VS 2008, I cannot load Firebug as it breaks the machine and is not supported. It took me several hours with tech support to repair my machine after installing FireFox/Bug. I could not find mention of tooltip anywhere nor find the inital value loaded. It did show me the correct field/# variable as I entered data in the field.
I just noticed your CssClass='myTextBox'. What is your code for this as I do not have a CssClass created for my textbox.
0

Something like this using jQuery:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
     <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#<%=DropDownList1.ClientID %>").change(function () {
                var tooltip = "B";
                var val = $(this).val();
                if (val == "One") {
                    tooltip = "A";
                }
                $("#<%=TextBox1.ClientID %>").attr('title', tooltip);
            });
        });
</script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:DropDownList ID="DropDownList1" runat="server">
        <asp:ListItem Text="One" />
        <asp:ListItem Text="Two" />
        <asp:ListItem Text="Three" />
    </asp:DropDownList>
    <asp:TextBox ID="TextBox1" runat="server" ToolTip="A"></asp:TextBox>
    </form>
</body>
</html>

Update - class selector way:

$(function () {
            $('.myddl').change(function () {
                var tooltip = "B";
                var val = $(this).val();
                if (val == "One") {
                    tooltip = "A";
                }
                $('.mytb').attr('title', tooltip);
            });
        });

 <asp:DropDownList ID="DropDownList1" runat="server" CssClass="myddl">

 <asp:TextBox ID="TextBox1" runat="server" ToolTip="A" CssClass="mytb"></asp:TextBox>

17 Comments

Even using the "#<%=TextBox1.ClientID %>" notation would not change the tooltip. For some reason that I cannot fathom, the code will not/can not access the textbox control. Looking at the "#TB" in the VS debugger, it's telling me the attributes are null; if I tried to change the tooltip by "#tb.attributes['title']" I got a runtime error. Document.getelementbyID returned null. I've tried using 1.5 JQuery with no effect. Something is preventing JQuery from modifying the ToolTip.
I expect it does. How are you running your code? IIs or in Visual Studio?
Visual Web Developer Express and I am sure it wouldn't make any difference when run on IIS.
They do work in different ways. I worked on a project in which I did all my development using IIs, all of the 'add-ins' that I was required to use had been tested using VS. NONE of them worked! The enviroment in this position will not allow for IIs to be used on my machine.
If you have VS or VWD Express 2010, you might want to try IIS Express. VWD and IIS express are free. For whatsoever reason if the clientID format didn't work you can try the class selector version. Check my another code-snippet.
|
0

The code I have listed in my question works and does change ToolTips. The problem lies with the person who created the site and his machinations of CSS and Javascript/JQuery/Json; somehow the ability to access ToolTips was disabled.

1 Comment

I finally got it to work, the problem was how the control was referenced. Using either '.mytb' or '#mytb' failed, I needed to use '[id$=mytb]'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.