Bug Tracker

Opened 10 years ago

Closed 8 years ago

Last modified 7 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 10 years ago by john

Resolution: wontfix
Status: newclosed
Type: enhancementfeature

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 10 years ago by frontier

Resolution: wontfix
Status: closedreopened

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

comment:3 Changed 10 years ago by john

Component: ajaxcore
Milestone: 1.1.31.1.4
Resolution: wontfix
Status: reopenedclosed
Version: 1.1.21.1.3

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

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

comment:4 Changed 10 years ago by rfsean

Resolution: wontfix
Status: closedreopened

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 9 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 8 years ago by john

Resolution: wontfix
Status: reopenedclosed

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

comment:7 Changed 7 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.