String handling in jQuery

I have a div that hits the text heading. I want to change the title from "+ filters" to "- filters" accordingly (when the div is arranged or expanded)

How can I accomplish this with jQuery:

if (headerDiv.text() starts with "+") replace "+" with "-" else replace "-" with "+" 

?

+6
jquery string
source share
3 answers

Nothing i got it

 if ($(header).text().trim().charAt(0) == "+") $(header).text($(header).text().replace("+", "-")); else $(header).text($(header).text().replace("-", "+")); 

If it is not, I would appreciate heads-up

+13
source share

I would do:

 var plus_string = "<span class=/"plus/">+</span>"; var minus_string = "<span class=/"minus/">-</span>"; if($("div.header").hasClass("plus")){ $("div.header").removeClass("plus"); $("div.header").addClass("minus"); $("div.header").append(plus_string); $("span.plus").remove(); } else (... the reverse of the above...) 

This is because I would use the plus and minus class to handle the css bit to change the appearance of the header header. Since you probably already do this, you can play a little with it as follows :)

+4
source share

keep it simple, why do you need to disassemble it? just replacing one line with another.

 if(headerDiv.text() == '+ filters') { headerDiv.text() == '- filters'; } elseif(headerDiv.text() == '- filters') { headerDiv.text() == '+ filters'; } 
+1
source share

All Articles