views:

103

answers:

1

I got this article for doing this but i found that this is not working as i have given multiple classes in same textbox. How to convert this to work with multiple classes. I dont know how to use selectors to work with.

I am using it like this

<div class="inbox3full">
    <div class="threeinbg"><asp:TextBox ID="txtSortOrder" CssClass="threein water" 
            Text='<%# Bind("SortOrder") %>' runat="server" ToolTip="Type your Sort Order"></asp:TextBox></div>
</div>

with jquery as

<script type="text/javascript">
//    $(document).ready(function() { 
        $(function() {

            $(".water").each(function() {
                $tb = $(this);
                if ($tb.val() != this.title) {
                    $tb.removeClass("water");
                }
            });

            $(".water").focus(function() {
                $tb = $(this);
                if ($tb.val() == this.title) {
                    $tb.val("");
                    $tb.removeClass("water");
                }
            });

            $(".water").blur(function() {
                $tb = $(this);
                if ($.trim($tb.val()) == "") {
                    $tb.val(this.title);
                    $tb.addClass("water");
                }
            });
        });
   // });
    </script>

EDIT Sys is undefined.

.water{font-family: Tahoma, Arial, sans-serif;font-size:75%; color:black;}

<script type="text/javascript">
    $(document).ready(function() {
        $(".water").addClass('watermark');
        $(".watermark").live('focus', function() {
            $tb = $(this);
            if ($tb.val() == this.title) {
                $tb.val("");
                $tb.removeClass("water");
            }
        }).live('blur', function() {
            $tb = $(this);
            if ($.trim($tb.val()) == "") {
                $tb.val(this.title);
                $tb.addClass("water");
            }
        }).blur();​
    });

    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function() {
      $(".water").not(document.activeElement).blur();
    });
    </script>
+2  A: 

Instead of an .each() like you have, it's easier to just fire the blur handler, like this (edited to use .live() since you're adding them in an UpdatePanel):

$(function() {
  $(".water").addClass("watermark");
  $(".watermark").live('focus', function() {
    $tb = $(this);
    if ($tb.val() == this.title) {
        $tb.val("");
        $tb.removeClass("water");
    }
  }).live('blur', function() {
    $tb = $(this);
    if ($.trim($tb.val()) == "") {
        $tb.val(this.title);
        $tb.addClass("water");
    }
  }).blur();​
});

You can see it working here. This puts the watermark/title in if the box was initially empty, which is usually what you want. Also when your PanelPanel finishes you'll want to call .blur() on those elements again to apply the watermark to freshly created ones, like this:

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function() {
  $(".water").not(document.activeElement).blur();
});

This will blur all elements except the currently focused one, as to not interrupt the user typing.

Nick Craver
@Nick: I tried above code. It is not working with my code although it is working over the place you shown. :(
Shantanu Gupta
@Shantanu - Are your elements dynamically added, via an UpdatePanel or something?
Nick Craver
@Nick: Yes, I am adding controls dynamically. And also using UpdatePanel with ajax 3.5
Shantanu Gupta
@Shantanu - You need to use `.live()` for that then, one moment and I'll update.
Nick Craver
@Nick: Does this `live()` works in both the cases or only if updatepanel is used
Shantanu Gupta
@Shantanu - It will work in both cases, it's a different approach to event handling. The trade-off it's less efficient for a small number of elements, but not much so...you just don't want *everything* (I mean hundreds here, not for your small numbers) using `.live()`, each one adds an event handler to `document` that has to react to every event of that type, and check the selector against the event target.
Nick Craver
@Nick: This script added by you has to be appended with the above script ?
Shantanu Gupta
Sad to say, dont know why it is still not working for me
Shantanu Gupta
I updated both scripts, make sure you're using the latest of the top. The button tells your updatePanel to run that function when a request completes, so it's just executing the `blur` handler for new elements. What isn't working, are you getting any errors in your console?
Nick Craver
@Nick: I am not getting any error. My buttons and textboxes are being generated dynamically using formview control of asp.net. NO effect is visible. See my edit for details
Shantanu Gupta
@Shantanu - Ah! you need a `document.ready` wrapper, check the update answer above and give it a shot. Also make sure the `water` CSS class has some properties like in my demo page, `.water { color: #aaa; }` for example.
Nick Craver
@Nick: After adding document.ready I am getting title in textboxes. But no blur effect is visible at this point. What is the role of `Sys.WebForms.PageRequestManager` in the code. I have not added this line. Where it needs to be added
Shantanu Gupta
@Shantanu - It can be added anywhere, right after the other script is fine. It's just telling it to apply the effect immediately to new controls added by the UpdatePanel. As I said in the last comment though, the `water` CSS class needs some properties to see any visible effect, the class alone won't add any visual styling unless you give it some properties.
Nick Craver
@Nick: First of all thanks for your patient. My css class is `.water{font-family: Tahoma, Arial, sans-serif;font-size:75%; color:black;}`
Shantanu Gupta
@Nick: When i clicks on textbox. content of textbox doesn't clears
Shantanu Gupta
@Shantanu - I had a complete brain lapse there try it now. It was removing the `water` class (which was fine before) and breaking the `.live()` handler for `blur` because it's looking for it. Adding a placeholder class that doesn't go away works, try the above it should solve the issue.
Nick Craver
Didn't worked. I added my latest script and css that I am using.
Shantanu Gupta
@Shantanu - In the code included after all of the other script? You can move it inside the `$(function() { });` wrapper to be safe.
Nick Craver
@Nick: Thanks for your support. I could not run this into my code. Dont know the reason so I am now using ajax watermark control in place of this but will try it at my home now.
Shantanu Gupta