tags:

views:

116

answers:

2

Hi, I'm using the jQuery TOOLS - The missing UI library. For tooltip on all input fields I do

      $("#univers :input").tooltip({
   effect: 'slide',
   position: "center right",
   offset: [-2, 10],
   effect: "fade",
   opacity: 0.5,
   tip: '.tooltip'
  });

But I would like to show tooltip only if input field is empty. So I did

          $("#univers input:text[value=""]").tooltip({
   effect: 'slide',
   position: "center right",
   offset: [-2, 10],
   effect: "fade",
   opacity: 0.5,
   tip: '.tooltip'
  });

But this one doesn't work !

Any idea ?

+3  A: 

That will only add the tooltip eventlistener to fields which are empty, it won't check if it is empty when you hover it.

I'm not familiar to the library you are using, and you did't post a link to it, but check if there is a callback function that is called just before the tooltip is displayed. Then you can check if the field is empty and return true or false depending to indicate if the tooltip should be shown or not.

EDIT

Have a look at the documentation for tooltip. There is a function called onbeforeshow, which does what I explained.

onBeforeShow: function() { 
  return (jQuery.trim($(this).val()) == "");
}
Marius
Yes thanks... I was trying find how to use it !I add :onBeforeShow: function() { // But what i have to put here about the input ?}
try this: onBeforeShow: function() { if(jQuery.trim($(this).val()) != "") { return false; } else { return true; } }
snuffer.ch
Thanks stuffer.ch, added it to the answer.
Marius
You're welcome - thanks to you - now I've learned a shorter version for my return code :-)
snuffer.ch
A: 

There are a few problems at play here.

First off, it has double quotes inside for the value, which means that it isn't valid syntax. You need to use single quotes so that the value will be a single string.

Try this:

$("#univers input[type=text][value='']")

Or even better:

$("#univers input:text[value='']")

Your second problem is that the selector doesn't automatically keep updating as fields get filled out. Therefore, you will be selecting all of the empty fields when the page loads, and even if they get filled out later, the callback will still get fired.

As Marius mentioned, you'll need to use the onbeforeshow callback to enable/disable the tooltip based on whether or not the field is actually empty.

Something like this should work:

// don't use [value=''] because a field that isn't empty at first might be empty later
$("#univers input:text").tooltip({
    effect: 'slide',
    position: "center right",
    offset: [-2, 10],
    effect: "fade",
    opacity: 0.5,
    tip: '.tooltip',
    onBeforeShow: function() {
        return this.value == ''; 
        // will return false if the value is not empty, 
        // returning false makes the tooltip not show
    }
});
TM
You need to remove the first part of the answer, or else it will only be added to fields which are originally empty, which might not be all of them.
Marius
@Marius look at my code sample at the bottom, I already removed it there and explained why it shouldn't be there. I only list it at the beginning to show the asker that you can't use double quotes within a double quoted string.
TM
Ok I understood the concept ;-)return this.value == '' doesn't work for me and I will try to find something else...Thanks to you