1

I'm facing a problem.

I have this:

<input type="hidden" name="Boss" id="Boss" value="8,116,167,198,139,203,158,170,">

Actually I have this code in js:

// On click on element with class .Boss
$("form").on("click", ".Boss", function(event){
  var clickedId = $(this).attr('value')+','; // give me 8,
  var locationBtn = $('#Boss'); // Select the input
  var locationBtnValue = $('#Boss').val(); // Take the select value

  if(locationBtnValue.toString().indexOf(clickedId) == -1) { locationBtn.val(locationBtnValue + clickedId); }
  else { locationBtn.val(locationBtnValue.replace(clickedId,'')); }
});

My problem is: if want to decide to remove the 8 my javascript do not remove the item 8, but the first occurrence it will find in my string, so 8,116,167,19**8,**139,203,158,170,. So it breaks my other item...

How can I make to do not break it ?

Thanks.

1
  • Your final outcome is not clear to me. What is the initial input and what is the final output? Commented Apr 2, 2015 at 20:15

3 Answers 3

1

I do not know what your final outcome is, but I think you want it to be 116,167,198,139,203,158,170, In this case you can split and filter the array to get rid of the value.

var str = "8,116,167,198,139,203,158,170,";  //the input
var updated = str.split(",")   //turn it into an array
                 .filter(function (val) { //loop through all the elements applying this function
                     return val!=="8"; //keep the item if the index does not match the item
                 }
              ).join(",");  //turn array back into a string
Sign up to request clarification or add additional context in comments.

Comments

0

One way to be consistent is by splitting it into an array and then removing the occurence.

// On click on element with class .Boss
$("form").on("click", ".Boss", function(event) {
  var clickedId = $(this).attr('value'); // give me 8
  var locationBtn = $('#Boss'); // Select the input
  var locationBtnValue = locationBtn.val(); // Take the select value
  var ids = locationBtnValue.split(','); //split into an array

  var index = ids.indexOf(clickedId); //index of clickedId inside ids

  if(index > -1) { //found
      ids = ids.splice(index, 1); //remove from ids

  } else {
      ids.push(clickedId); //add to ids
  }

  locationBtn.val(ids.join(','));      

});

1 Comment

Hello, thanks for your idea but if I click it gain, it do not keeps old datas. I have just the current clicked one.
0

That's what replace does when you pass it a string, it replaces the first occurrence.

You need to pass it a regular expression with the global modifier, like this

locationBtnValue.replace(/8,/g,'')

you can do the same thing with the RegExp constructor, and create a regular expression from the string you have

var clickedId = $(this).val() + ',';

var regex = new RegExp(clickedId, "g");

locationBtnValue.replace(regex,'');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.