Sriyani Rathnayaka
Sriyani Rathnayaka

Reputation: 107

display text box hint in two text boxes

I use this jquery function to display a textbox hint

this is function

function textboxHint(id, options) {
    var o = { selector: 'input:text[title]', blurClass:'blur' };
    $e = $('#'+id);
    $.extend(true, o, options || {});

    if ($e.is(':text')) {
      if (!$e.attr('title')) $e = null;
    } else {
      $e = $e.find(o.selector);
    }
    if ($e) {
      $e.each(function() {
      var $t = $(this);
      if ($.trim($t.val()).length == 0) { $t.val($t.attr('title')); }
      if ($t.val() == $t.attr('title')) {
    $t.addClass(o.blurClass);
      } else {
        $t.removeClass(o.blurClass);
      }

     $t.focus(function() {
    if ($.trim($t.val()) == $t.attr('title')) {
      $t.val('');
      $t.removeClass(o.blurClass);
    }
    }).blur(function() {
      var val = $.trim($t.val());
      if (val.length == 0 || val == $t.attr('title')) {
        $t.val($t.attr('title'));
        $t.addClass(o.blurClass);
      }
    });

         // empty the text box on form submit               
    $(this.form).submit(function(){
      if ($.trim($t.val()) == $t.attr('title')) $t.val('');
    });
   });
 }
}

this is html

<form action="" method="" class="search" >
    <input type="text"  name="search" class="text"  title="Search Subjects..." id="block" />
    <input type="submit"  name="submit"  value="Search" class="submit" />
</form>

I called function like this..

$(document).ready(function() {
    textboxHint("block");
});

This is working.. but the problem is now I need to use this function to display a hint in two different text box with two different Ids. Eg: Id = block1 and Id = block2 etc...

Can anybody help me in modifying this function to do this?

Upvotes: 3

Views: 212

Answers (2)

Akhil Sekharan
Akhil Sekharan

Reputation: 12683

How about modifying the function to receive a selector and loop through each elements:

function textboxHint(selector, options) {
    $(selector).each(function(){
        var o = { selector: 'input:text[title]', blurClass:'blur' };
        $e = this;
        $.extend(true, o, options || {});

        if ($e.is(':text')) {
          if (!$e.attr('title')) $e = null;
        } else {
          $e = $e.find(o.selector);
        }
        if ($e) {
          $e.each(function() {
          var $t = $(this);
          if ($.trim($t.val()).length == 0) { $t.val($t.attr('title')); }
          if ($t.val() == $t.attr('title')) {
        $t.addClass(o.blurClass);
          } else {
            $t.removeClass(o.blurClass);
          }

         $t.focus(function() {
        if ($.trim($t.val()) == $t.attr('title')) {
          $t.val('');
          $t.removeClass(o.blurClass);
        }
        }).blur(function() {
          var val = $.trim($t.val());
          if (val.length == 0 || val == $t.attr('title')) {
            $t.val($t.attr('title'));
            $t.addClass(o.blurClass);
          }
        });

             // empty the text box on form submit               
        $(this.form).submit(function(){
          if ($.trim($t.val()) == $t.attr('title')) $t.val('');
        });
       });
    }   
    });
}

And calling it like:

<form action="" method="" class="search" >
    <input type="text"  name="search" class="text"  title="Search Subjects..." class="block" id="block1" />
    <input type="text"  name="search" class="text"  title="Search Subjects..." class="block" id="block2" />
    <input type="submit"  name="submit"  value="Search" class="submit" />
</form>

$(document).ready(function() {
    textboxHint(".block");
});

Upvotes: 2

happy
happy

Reputation: 472

What about this:

$(document).ready(function() {
    textboxHint("block1");
    textboxHint("block2");
    textboxHint("block3");
});

As you can see, since the function ask for id as an argument, you can supply different id by calling the function more than once.

Regards

Upvotes: 4

Related Questions