jQuery: The Write Less, Do More JavaScript Library

Ticket #1092 (reopened feature)

Opened 1 year ago

Last modified 1 year ago

ToggleText function requested

Reported by: frontier Assigned to: anonymous
Type: feature Priority: major
Milestone: 1.1.4 Component: core
Version: 1.1.3 Keywords:
Cc: Needs: Review

Description

There are lots of other useful toggle functions in JQuery, I think one for Text is needed also. Thanks :)

Attachments

Change History

Changed 1 year ago by john

  • status changed from new to closed
  • type changed from enhancement to feature
  • resolution set to wontfix

I'm not entirely sure what you're requesting, specifically - but this sounds like something that could be easily duplicated otherwise.

jQuery.fn.textToggle = function(){
  return this.each(function(){
    if ( !this.oldValue ) {
      this.oldValue = jQuery(this).text();
      jQuery(this).text('');
    } else
      jQuery(this).text( this.oldValue );
  });
};

Changed 1 year ago by frontier

  • status changed from closed to reopened
  • resolution deleted

how do I use that? sorry I'm not a JS guru

Changed 1 year ago by john

  • status changed from reopened to closed
  • component changed from ajax to core
  • version changed from 1.1.2 to 1.1.3
  • resolution set to wontfix
  • milestone changed from 1.1.3 to 1.1.4

You would include that in your page, then you would call it like so:

  $("#myelem").textToggle();

Changed 1 year ago by rfsean

  • status changed from closed to reopened
  • resolution deleted

The solution posted only works once, since the variable is defined on DocumentReady?. This is unacceptable, as toggle(Function even, Function odd) will toggle functions ad infinitum. Unfortunately, the following does not work:

$(.class).click(function() {
  $(.class).toggle(function() {
    $(this).text('to');},
  function() {
    $(this).text('fro');}
  );
});

This is what I assume the OP is trying to accomplish, as I am trying to accomplish the same.

Example usage is toggling of an element with a text-based link, having text switch between "open" and "close", "show" and "hide", etc.

Note: See TracTickets for help on using tickets.