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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:13:28+00:00 2026-05-26T11:13:28+00:00

I have a string where part of the string depends on whether the variable

  • 0

I have a string where part of the string depends on whether the variable json[i].has_dishwasher contains a value. If it contains a value, class="selected" will be added to the html string. To make my code shorter (I actually have a long series of the code shown below), i am planning to use the ternary if operator.

Problem: The way I used the ternary if conditional statement is not valid Javascript. How can I make this valid?

Javascript Code

html = '<div class="amenities"' + (json[i].has_dishwasher ? +'class="selected"'+) \
+ '>Dishwasher' +  json[i].has_dishwasher + '</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-26T11:13:28+00:00Added an answer on May 26, 2026 at 11:13 am

    You need to add the “tri” part of the ternary, and it should return just the strings, not include the concatenation (+) operators.

    ... + (json[i].has_dishwasher ? 'class="selected"' : '') + ...
    

    But that isn’t really what you want either; that would leave you with two separate “class” attributes. I’d probably create the “class” attribute’s value apart from the creation of the HTML string:

    var classVal = "amenities" + (json[i].has_dishwasher ? " selected" : "");
    

    Or just do it in a conditional and keep it clean.

    var classVal = "amenities";
    if (json[i].has_dishwasher) classVal += " selected";
    
    html = '<div class="' + classVal + '>Dishwasher' 
         + json[i].has_dishwasher + '</div>';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have string value and Type of some variable (For example int and 32
I have a class: class Cars { String Part; int NrOfParts; } and a
Let's presume that I have string like '=&?/;#+%' to be a part of my
i have string stored in python variables, and i am outputting a html that
I have string like this first#second, and I wonder how to get second part
I have a URL that also might have a query string part, the query
I have a module that uses std::string as part of its interface. I would
I have the following 'td'element, and want to read out the string part (cell
i have the string which is a part of an xml. a<b>b</b>c<i>d</i>e<b>f</b>g the problem
I have a tag <AboutUs> (having HTML tags) and I have stored the string

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.