Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8415351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:25:19+00:00 2026-06-10T01:25:19+00:00

I have a title and a unorder list <h2> here is some interesting links

  • 0

I have a title and a unorder list

<h2> here is some interesting links </h2>
 <ul>
  <li>link 1</li>
  <li>link 2</li>
  <li>link 3</li>
 </ul>

what I like is to add a [+] at the end of the  tag, and hide the following

    then on click on h2, show it…. BUT i need to mak e the [+] become a [-]

    what is the best way to change that ?

    it will look like :

    closed : title [+]

    open : title [-]

  • -link1
  • -link2
  • -link3
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T01:25:21+00:00Added an answer on June 10, 2026 at 1:25 am

    Assuming no changes to your current HTML, and all modifications through jQuery:

    $('h2').html(
        function(i,h) {
            return h + '<span>[+]</span>';
        }).on('click', 'span', function(){
            var that = $(this);
            that.closest('h2').next('ul').toggle();
            that.text(
                function(){
                    return that.text().indexOf('+') !== -1 ? '[-]' : '[+]';
                });
        });​
    

    JS Fiddle demo.


    Edited to explain this part of the above code:

     return that.text().indexOf('+') !== -1 ? '[-]' : '[+]';
    

    This is a ternary operator, it evaluates a condition (return that.text().indexOf('+') !== -1) for being true, or false. Following the ? is the ‘if-true’ response, and following the : is the ‘if-false’ response.

    So, the condition assesses whether or not the + character is found within the string returned from the text() of the current element. We’re comparing the result to -1 because indexOf() returns the index of the found substring. Because the substring can be found at 0 (the beginning of the string), indexOf() returns -1 when the string is not found. Therefore if the + is found (so the returned result will not be equal to -1) within the text of the span element the text is changed to [-], whereas if the + is found the text will be changed to [-].

    The return statement simply returns the changed-text to the text() method.


    Updated to amend the text() method’s function, and take advantage of the fact that the text of the element is available as the second parameter in the function:

    $('h2').html(
        function(i,h) {
            return h + '<span>[+]</span>';
        }).on('click', 'span', function(){
            var that = $(this);
            that.closest('h2').next('ul').toggle();
            that.text(
                function(i,t){
                    return t.indexOf('+') !== -1 ? '[-]' : '[+]';
                });
        });​
    

    JS Fiddle demo.

    References:

    • closest().
    • html().
    • next().
    • on().
    • JavaScript ternary operator.
    • text().
    • toggle().
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I like to have the title tag caption over a float list of image...
I want to have my title tag look like this: My Website - Name
I would like to take a list of RawDoc which each have a title
I would like to have a title element with overflow-x:hidden and overflow-y:visible . However,
I have 4 tables and this query SELECT tag.id, title FROM tag LEFT JOIN
I've made this html box, that is supposed to have a title and some
For example I have title The Battleship Trailer so it will generate keyword like
I have a title and subtitle like so: The Programmer Saving the world with
I have a list of elements in Sharepoint, they have title, description and the
I would like to create form with text_fields TITLE CONTENT TAGS I have Post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.