triplethreat77
triplethreat77

Reputation: 1296

Update number on jQuery UI tooltips

I have folders with tooltips such as '0 entries' or '5 entries' and so on. I need this tooltip number to update by 1 every time something is dropped into the folder. The title doesn't always start at zero, and I need $(this) drop div updated, as I have many. Here is the working fiddle http://jsfiddle.net/4ehSG/3

jQuery

$(document).tooltip();

var dropped =0;

$( ".draggable" ).draggable();
    $( ".droppable" ).droppable({
      drop: function( event, ui ) {
        dropped++;
          $( this )
          .attr('title',dropped+' entries')
          .addClass( "ui-state-highlight" )
          .find( "p" )
            .html( "Dropped!" );
          $( ".draggable" ).fadeOut();
      }
    });

HTML

<div class="draggable ui-widget-content">
  <p>Drag me to my target</p>
</div>

<div class="droppable ui-widget-header" title='2 entries'>
  <p>Drop here</p>
</div>

Upvotes: 0

Views: 74

Answers (5)

Dom
Dom

Reputation: 40459

If you have multiple instances of droppable and draggable, you may want to give each droppable an array associated with it. That way you don't need to rely on a count object and you could drop the same draggable on multiple droppable objects.

DEMO

$(document).tooltip();
$( ".draggable" ).draggable();
$( ".droppable" ).droppable({
    drop: function( event, ui ) {
        if(!$(this).data('droplist')){ //check for array
            $(this).data('droplist', []); //if doesn't exist, create array
        }
        var droplist = $(this).data('droplist'),
            drag = $(ui.draggable)[0];

        if(droplist.indexOf(drag) === -1) //check if element exists in array
            droplist.push(drag);

        $( this )
        .addClass( 'ui-state-highlight' )
        .find( 'p' )
        .html( 'Dropped!' )
        .end()
        .attr('title', droplist.length + ' entries');

         $(this).data('droplist', droplist); //set list
    }
});

Upvotes: 1

dwaddell
dwaddell

Reputation: 1496

Here is an example of what you can do: http://jsfiddle.net/4ehSG/9/

  drop: function( event, ui ) {
      var dropped = parseInt($(this).attr('title')) + 1;
      $( this )
      .attr('title',dropped+' entries')
      .addClass( "ui-state-highlight" )
      .find( "p" )
        .html( "Dropped!" );
      $( ".draggable" ).fadeOut();
  }

Upvotes: 1

Frax
Frax

Reputation: 3093

You can use:

document.getElementById('droppable').title = value;

The above line of code is without using jQuery.

If you want to use jQuery, use the following:

$("#droppable").attr( 'title', value );

Upvotes: 0

DEMO

$(document).tooltip();

var count = 0;

$("#draggable").draggable();
$("#droppable").droppable({
    drop: function (event, ui) {
        count++;
        $(this)
            .attr('title', count + ' entries')
            .addClass("ui-state-highlight")
            .find("p")
            .html("Dropped!");
        $("#draggable").fadeOut();
    }
});

Upvotes: 0

lionheart98
lionheart98

Reputation: 968

You could increase a variable every time an element is dropped

try this

$(document).tooltip();
var num = 0;
$( "#draggable" ).draggable();
$( "#droppable" ).droppable({
    drop: function( event, ui ) {
        $( this )
        .addClass( "ui-state-highlight" )
        .find( "p" )
        .html( "Dropped!" );
        num++;

        $( "#draggable" ).fadeOut();
        $( "#droppable" ).attr("title", num + " entries");
  }
});

your updated example: http://jsfiddle.net/4ehSG/4/

Upvotes: 1

Related Questions