views:

26

answers:

1

I have a small javascript that changes the title of a input-field to it's value and some other stuff:

  function autoFill(id){
   if(jQuery(id).val()==""){
                    jQuery(id).val(jQuery(id).attr("title"))
                    .addClass("help");
             }; 
    jQuery(id).focus(function(){
    if(jQuery(this).val()==jQuery(this).attr("title")){
     jQuery(this).val("").removeClass("help");
    }
   })
   .blur(function(){
    if(jQuery(this).val()==""){
     jQuery(this).val(jQuery(this).attr("title"))
     .addClass("help");

    }
   });
   jQuery(".trip").submit(function(){
    if(jQuery(id).val() == jQuery(id).attr("title")){
    jQuery(id).val(''); 
  }

 });
  }

When I try to use this script on a class that is on several nodes on one page it only works on the first. For example:

autoFill(".field");

Now I have to make it like this instead:

 autoFill("#driver_from");
  autoFill("#driver_to");
  autoFill("#driver_when");
  autoFill("#passenger_from");
  autoFill("#passenger_to");
  autoFill("#passenger_when");

how do I make it so that it works on every field instead?

+4  A: 

Something like this would work:

function autoFill(selector){
  jQuery(selector).each(function() {
    if(jQuery(this).val() == "") {
      jQuery(this).val(jQuery(this).attr("title"))
                  .addClass("help");
    }; 
    jQuery(".trip").submit(function(){
      if(jQuery(this).val() == jQuery(this).attr("title")) {
         jQuery(this).val(''); 
      }
    });
  }).focus(function(){
     if(jQuery(this).val() == jQuery(this).attr("title")) {
        jQuery(this).val("").removeClass("help");
     }
  }).blur(function(){
     if(jQuery(this).val() == "") {
        jQuery(this).val(jQuery(this).attr("title"))
                    .addClass("help");
     }
  });
}

The important part is the .val() check at the beginning, it's getting the .val() of the first match, you need to handle each separately.


Or, rewrite it as a plugin like this:

(function($) {    
  $.fn.autoFill = function() {
     return this.each(function() {
       $(".trip").submit(function(){
         if($(this).val() == $(this).attr("title")) {
           $(this).val(''); 
         }
       });
     }).focus(function(){
        if($(this).val() == $(this).attr("title")) {
           $(this).val("").removeClass("help");
        }
     }).blur(function(){
        if($(this).val() == "") {
           $(this).val($(this).attr("title")).addClass("help");
        }
     }).blur();
  };
})(jQuery);

Then you can call it like this:

$(".field").autoFill();
Nick Craver
it doesn't seem to work, here is a html example:<input type="text" class="field from" id="from" name="from" value="Ort, gata eller kommun">Here is the calling of the funtion: autoFill(".field");and the rest is copypaste from your fuction!
Kristoffer Nolgren
@Kristoffer - That element has no title in it....what is your *expected* result when that's the case? Here's that same element with a title added: http://jsfiddle.net/nick_craver/85Jgr/
Nick Craver
yeah, sorry, i did it wrong in my templates, here is what it looks like now: <input type="text" class="field from" id="from" name="from" title="Ort, gata eller kommun">still not working...dev.resihop.nu is an url for the site, it's a probono ride-sharing search-engine ;)
Kristoffer Nolgren
@Kristoffer - Your site's throwing another JavaScript error, take a look at your console, it's happening here: `kundo.create_button();`
Nick Craver
ah, right, it's gone now, didn't have anything todo with this...
Kristoffer Nolgren
@Kristoffer - Ah you copied the code immediately, I saw/corrected this just after it seems. Your code in the page looks like this: `if(jQuery(id).val()==""){ jQuery(id).val(jQuery(id).attr("title")).addClass("help"); };`, make sure it matches the answer above, then try :)
Nick Craver
awesome! Superawesome!
Kristoffer Nolgren