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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:06:44+00:00 2026-05-29T04:06:44+00:00

I have a returned JSON string which I have parsed and put through into

  • 0

I have a returned JSON string which I have parsed and put through into an array and then formatted to put into a select dropdown list:

   function populateGroups(){
        var categories = '';
        for( category in gCategories ){
            categories += '<option name="' + gCategories[category] + '">' + gCategories[category] + '</option>';
            categories.replace(' ', '_');
        }
        $('select[name="category"]').html(categories);    
    }

The problem I have is that the option names (which are the same as the values) contain spaces which I need to replace with something that would be valid, such as an underscore ‘_’.

That is why I have tried doing the categories.replace(‘ ‘, ‘_’); I have also tried doing this with regex, although my RegEx is not very good 😛

Any ideas on how this would be done?

Thanks!

[Note]
Here is the JSFiddle : http://jsfiddle.net/pKYr4/

  • 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-29T04:06:45+00:00Added an answer on May 29, 2026 at 4:06 am

    You should update the string as well;

    categories = categories.replace(/ /g, '_');
    

    …a problem though is that not just the values are formated/trimmed as <option name="... will be affected as well.

    <option_name="category_value">category_value</option>
    

    This can be solved this way;

    function populateGroups() {
        var categories = '';
        for( c in gCategories ) {
            var categoryValue = gCategories[c].replace(/ /g, '_');
            categories += '<option name="' + categoryValue + '">' + gCategories[c] + '</option>';
        }
        $('select[name="category"]').html(categories);    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some Json being returned from facebook which I'm then parsing in to
I’m new to android,I have an activity class which fetches Json string from url
I have a very large integer returned by a json string from my server
Hi I have a List which is returned by a webmethod. This needs to
Ok, a little new to JSON format.. I have the following JSON string returned
I have some JSON returned to the browser like this product: { Title: School
I have an HTTP Module that I use to clean up the JSON returned
Which Activator.CreateInstance overload function to call? I have a type returned from Type proxyType
I have a List being returned from a daoTemplate.query() call, using a rowMapper .
I have a string being returned from my database that I want to use

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.