Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String manipulation in jQuery

Tags:

jquery

string

I have a div which collapses clicking on a text header. I want to change the header from "+ filters" to "- filters" accordingly (when the div is collapsed or expanded)

How can I perform this with jQuery?

if (headerDiv.text() starts with "+")
    replace "+" with "-" 
else 
    replace "-" with "+"
like image 886
juan Avatar asked Mar 02 '09 13:03

juan


1 Answers

Never mind, I figured it out

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

If this is not the correct way, I'd appreciate a heads up

like image 52
juan Avatar answered Sep 21 '22 13:09

juan