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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:33:51+00:00 2026-05-28T16:33:51+00:00

I am trying to build a toggle switch which on click add or removes

  • 0

I am trying to build a toggle switch which on click add or removes white-space wrap.

The div that has to wrap cannot have and ID, so it needs to be identified via it’s class. In this case ‘text’. On the entire page, that class only gets used once.

I build this, but it doesn’t work. Where do I go wrong?

<script>
function togglew(){    
     if(document.getElementsByClassName("text")[0].style.whiteSpace == 'nowrap'){
      document.getElementsByClassName("text")[0].style.whiteSpace == 'normal';
     }else{
        document.getElementsByClassName("text")[0].style.whiteSpace == 'nowrap';
     }
}
</script>

<a onclick="togglew('');">toggle wrap button</a> 
<br>---<br>
<div style="width:500px;">
    <div class="text" style="overflow:auto;white-space:wrap">ssssssssssd fdfsdfsdf sdf sdf sdf sfd sdf sdf sdf ssdfdddddddddddf sdf sdf sdf sdfsd fsdf sdfsdf sdf sdfsd f</div>
</div>
  • 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-05-28T16:33:52+00:00Added an answer on May 28, 2026 at 4:33 pm

    == is a comparison operator where as = is an assignment operator.

    Inside the if condition you should be comparing (==), and inside the blocks you should be assigning (=);

    if(document.getElementsByClassName("text")[0].style.whiteSpace == 'nowrap')
    {
        document.getElementsByClassName("text")[0].style.whiteSpace = 'normal';
    } else {
        document.getElementsByClassName("text")[0].style.whiteSpace = 'nowrap';
    }
    

    For completeness, there is also the === comparison operator, which also checks that the operators are of-the-same type (== does type coercion if the operands are of different types). For more info, see Which equals operator (== vs ===) should be used in JavaScript comparisons?.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to build a little toggle switch which removes line numbers on
I'm trying to build a really simple generic toggle functionality where the toggling switch
Im trying to build a countdown, which has four sections: to be displayed like
I am trying to build a topbar that has some links on the left
I'm trying build a method which returns the shortest path from one node to
I'm trying to build some HTML with Knockout that Jquery UI can turn into
I'm trying to toggle the build_files_toggle classed div in the next row when you
I'm trying build a regex that will replace any characters not of the format:
Trying to build a plugin effect that will some what look better than this
Trying to build a micro-CMS (of sorts), which needs to dish out content i.e.

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.