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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:27:04+00:00 2026-06-08T18:27:04+00:00

So basically here is my jsFiddle – http://jsfiddle.net/CmNFu/ . And code also here –

  • 0

So basically here is my jsFiddle – http://jsfiddle.net/CmNFu/ .

And code also here –

HTML –

<b style="float: left; margin-right: 10px;">category 1</b><input type="checkbox" value="category1" style="float: left;" class="portfolio-category" /><br />
<b style="float: left; margin-right: 10px;">category 2</b><input type="checkbox" value="category2" style="float: left;" class="portfolio-category" /><br />
<br />
<br />
<input type="text" name="categories" id="portfolio-categories" />​

jQuery –

jQuery(document).ready(function() {
    jQuery(".portfolio-category").click(function() {
        if(jQuery(this).is(":checked")) {
            jQuery("#portfolio-categories").val(jQuery("#portfolio-categories").val()+" "+jQuery(this).val());
        }
        else {
            var portfolioCategories = jQuery("#portfolio-categories").val();    
            alert("before + "+portfolioCategories);
            var currentElement = jQuery(this).val()+" ";
            alert(currentElement);
            portfolioCategories = portfolioCategories.replace(currentElement, "");
            alert(portfolioCategories);
        }
    });
});

​Well basically what I would like to achieve is, when user checks the checkbox, the value automatically adds inside input field (Done, it’s working, whooray!), but the problem is when it unchecks the checkbox, the value should be removed from input box (the problem starts here), it doesn’t remove anything. You can see I tried assigning val() function to variables, but also without success. Check my example on jsFiddle to see it live.

Any suggestions? I guess replace() is not working for val(), is it?

So, is there any other suggestions?

  • 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-08T18:27:05+00:00Added an answer on June 8, 2026 at 6:27 pm

    You have quite the issue with spaces in that input box. but we’ll get to that in a moment.

    first, this will kind of work (if it weren’t for the spaces problem):

    add this line before the last alert:

     jQuery("#portfolio-categories").val(portfolioCategories);
    

    this will work, but not always, as the last element you append doesn’t have a space after it.

    but if you change the 4th line to this:

    jQuery("#portfolio-categories").val(jQuery("#portfolio-categories").val()+jQuery(this).val()+" ");
    

    it will work, as it adds the space after each element, instead of before.

    http://jsfiddle.net/CmNFu/5/

    your issue was that you changed the values in the variable: portfolioCategories, but you haven’t updated the input itself. (notice, changing the value of a string, doesn’t change the value of the input it originally came from)

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

Sidebar

Related Questions

here is the code: http://jsfiddle.net/hQnXm/7/ So basically i need when hover, it should animate
here is the code: http://jsfiddle.net/4WyPq/ So basically when I click 'button1' it animates then
here is the code: http://jsfiddle.net/duNHJ/2/ Basically there is an animations every div so when
I'm using this plugin and here is the code: http://jsfiddle.net/gm8t5/ Basically I want to
You can play with my code here: http://jsfiddle.net/gqQnd/ Basically I want to have a
so basically here is my script: http://jsfiddle.net/JJFap/42/ Code - jQuery(document).ready(function() { var rel =
All of this is code is here: http://jsfiddle.net/yrgK8/ I have a news section which
Ok so here is my jsfiddle - http://jsfiddle.net/8947F/ basically the slideToggle seems to add
jsFiddle here: http://jsfiddle.net/_mtr/uqMZJ/ Basically, a table with rows, only 10 or so are not
http://jsfiddle.net/qtt3W/ I put my problem into the jsfiddle here. Basically what my goal is,

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.