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 8362641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:59:18+00:00 2026-06-09T11:59:18+00:00

I know this code is messy: $(#tog-desc).click(function(e) { $(this).toggleClass(open); $(.description).slideToggle(); if( $(this).hasClass(open) ) {

  • 0

I know this code is messy:

$("#tog-desc").click(function(e) {
    $(this).toggleClass("open");
    $(".description").slideToggle();

    if( $(this).hasClass("open") ) {
        $(this).html("Hide Description").css("background-position", "85px 3px");
    }
    else {
        $(this).html("Show Description").css("background-position", "88px -10px");
    }

    e.preventDefault();
});

But I’m not sure how this could be tidied up with the ternary operator to make it neat and concise. I always seem to end up writing overly long if/else statements with Javascript and I’d like to understand how not to have to do so!

Edit: by default #tog-desc shows “Show Description” and .description is hidden, in case this wasn’t obvious

Thanks.

  • 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-09T11:59:19+00:00Added an answer on June 9, 2026 at 11:59 am
    $("#tog-desc").click(function(e) {
        var $this = $(this), has_cls = $this.hasClass("open");
        $this.toggleClass("open")
           .text((has_cls ? "Hide" : "Show") + " Description")
           .css("background-position", has_cls ? "85px 3px" : "88px -10px");
    
        $(".description").slideToggle()
    
        e.preventDefault();
    });
    

    Or make the background-position part of the .open css class…

    $("#tog-desc").click(function(e) {
        var $this = $(this);
        $this.toggleClass("open")
           .text(($this.hasClass("open") ? "Hide" : "Show") + " Description");
    
        $(".description").slideToggle();
    
        e.preventDefault();
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have this code in my view.. <%=Html.DropDownList(ProductTemplate,new SelectList(Model.ProductTemplate,Value,Text))%> I know if this
With this code function someFunction(classParam:Class):Boolean { // how to know if classParam implements some
I know my code is messy, I am very new to this and am
I know this code use to work, I must have accidentally changed something when
Does anyone know why this code doesn't work. This means, the alert is NOT
Hello everyone don't know why this code works on windows but not on linux
I know this sort of code is not best practice, but nevertheless in certain
With this code I can download the file but I should know the file
I know that this line of code will make the cell text-wrap: $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setWrapText(true); 'D1'
I have this code and don't know if what I would like to achieve

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.