1

I use javascript (jQuery) to toggle text of html-element (span):

function TextToogle(element, text) {
   if (element.html() == '-' + text) {
       element.html(text.replace(element.html(), '+' + text));
   }
   else {
       element.html(text.replace(element.html(), '-' + text));
   }
}

I give two arguments for my function: 1) element - html Object 2) text - default text of span

My goal is toggle the span text from "+text" to "-text" and vice versa.

But script doesn't work correctly. When function toggle text to "+text", as result I see "text". The toggle to "-text" works correctly.

8
  • Can you create a fiddle: jsfiddle.net? Commented Feb 27, 2014 at 11:29
  • 11
    you have made a simple problem very complicated Commented Feb 27, 2014 at 11:29
  • Dealing with text.. very unreliable approach. Commented Feb 27, 2014 at 11:30
  • Rahul Desai, Yes, I can. I will attach link after 5 minutes. Commented Feb 27, 2014 at 11:36
  • dfsq, What is better approach to solve my task? Commented Feb 27, 2014 at 11:37

5 Answers 5

4

I would just replace inside a callback, seems so much simpler

element.text(function(_, txt) {
    return txt.replace(/[+-]/g, function(x) {
        return x == '+' ? '-' : '+';
    });
});

FIDDLE

Sign up to request clarification or add additional context in comments.

Comments

3

Based on you is comparison (element.html() == '-' + text) your function can be simpler. You don't need text.replace(element.html(), '+' + text) part:

function TextToogle(element, text) {
   if (element.text() == '-' + text) {
       element.text('+' + text);
   }
   else {
       element.text('-' + text);
   }
}

However relying on text comparison is not ideal. I would use CSS which is not text dependent:

$('.text-toggle').click(function() {
    $(this).toggleClass('active');
});

CSS

.text-toggle:before {
    content: '+';
}
.text-toggle.active:before {
    content: '-';
}

There is obvious advantage here is that you can style your +/- easily, you can set a background image, chage font-size, etc. While you would'n be able this with your original approach.

Demo: http://jsfiddle.net/dfsq/mvj5c/

1 Comment

And you are first. Thanks.
0
function toggleText(text) {
    return {'-':'+', '+':'-'}[text[0]]+text.slice(1);
}

element.html(toggleText(element.html()));

Comments

0

See simple example.

You've text -abc. Now it's fail on both of the checks.

Why not do this

function TextToogle(element, text) {
   var check = text.splice(0, 1);
   if (check === "-") {
       element.html("+" + text.substring(1, text.length));
   }
   else {
       element.html("-" + text.substring(1, text.length));
   }
}

Comments

0

try this code

$('span').click(function(){
  var 
     $this = $(this),
     thisText = $this.text();
  $this.text($this.text() === "-" + thisText ? ("+" + thisText) : ("-" + thisText));

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.