Bug Tracker

Opened 9 years ago

Closed 7 years ago

Last modified 6 years ago

#1092 closed feature (wontfix)

ToggleText function requested

Reported by: frontier Owned by:
Priority: major Milestone: 1.1.4
Component: core Version: 1.1.3
Keywords: Cc:
Blocked by: Blocking:

Description

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

Change History (7)

comment:1 Changed 9 years ago by john

  • Resolution set to wontfix
  • Status changed from new to closed
  • Type changed from enhancement to feature

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 );
  });
};

comment:2 Changed 9 years ago by frontier

  • Resolution wontfix deleted
  • Status changed from closed to reopened

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

comment:3 Changed 9 years ago by john

  • Component changed from ajax to core
  • Milestone changed from 1.1.3 to 1.1.4
  • Resolution set to wontfix
  • Status changed from reopened to closed
  • Version changed from 1.1.2 to 1.1.3

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

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

comment:4 Changed 9 years ago by rfsean

  • Resolution wontfix deleted
  • Status changed from closed to reopened

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.

comment:5 Changed 8 years ago by sgh445

jQuery.fn.toggleText = function(a, b) {
	return this.each(function() {
		jQuery(this).text(jQuery(this).text() == a ? b : a);
	});
};

comment:6 Changed 7 years ago by john

  • Resolution set to wontfix
  • Status changed from reopened to closed

We're not looking to land this at the time being.

comment:7 Changed 6 years ago by dwaynecharrington@…

I just encountered a situation where such a function being in the jQuery core would have been a lifesaver and saved a whole lot of extra code / writing a plugin.

Think of the following scenario:

You have a container div and inside you have a header and a body div. The sole purpose is to allow you to toggle the body sections visibility (collapsible sections).

What happens when you want to show and hide a section and want a link within the header to change accordingly? Show more and Show less for example? You have to write the above code in, jQuery should have this by default.

Please consider adding this.

Note: See TracTickets for help on using tickets.