2

Given an input like:

<input type="test" value="3,4,9" />

What's the best way to remove a value like 9, 4 or 3, without having issues with the commas, I don't want this ending up:

value="3,4,"
value="3,,9"
value=",4,9"

Is there a clean way to get this done in JavaScript/jQuery?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
TheExit
  • 5,220
  • 12
  • 37
  • 49

9 Answers9

7

You could split your value into an array, then filter out values you do not want.

$("input[type='test']").val().split(",") // ["3","4","9"]
.filter(function(v){return !isNaN(parseInt(v))})    // filter out anything which is not 0 or more

Here is a less terse version which filters out anything which is not numeric

var array = $("input[type='test']").val().split(",");

// If you are dealing with numeric values then you will want
// to cast the string as a number
var numbers  = array.map(function(v){ return parseInt(v)});

// Remove anything which is not a number
var filtered = numbers.filter(function(v){ return !isNaN(v)});

// If you want to rejoin your values
var joined = filtered.join(",");

Finally change the value on the input

$("input[type='test']").val(joined);
The Who
  • 6,552
  • 5
  • 36
  • 33
  • nice, but in this case, 4 is a variable? how do you make 4 a js var? – TheExit Jan 03 '11 at 20:26
  • `filter` takes a function which returns true or false. If true the value is kept in the array, if false it is removed. So adjust the filter function to your needs. Make sense? – The Who Jan 03 '11 at 20:29
2

Similar to PHP implode/explode functions

Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};

var explode = value.split(',');
explode.remove(1);
var implode = explode.join(',');

Documentation:
fce: Split
fce: Join
fce: Array.remove

Marek Sebera
  • 39,650
  • 37
  • 158
  • 244
  • Interesting but I don't see how lines 1 and 3 are connected throughout your example? – TheExit Jan 03 '11 at 20:20
  • That ends up breaking: ["3", undefined, "9"] 3,,9 outputed by var explode = values_input.val().split(','); delete explode[1]; var implode = explode.join(','); console.log(explode); console.log(implode); – TheExit Jan 03 '11 at 20:24
  • @TheExit: position of number 4 (1st element has index 0) – Marek Sebera Jan 03 '11 at 20:25
  • using John Resig's remove function on Array's prototype, you can remove it better, but jQuery solution is way better :) – Marek Sebera Jan 03 '11 at 21:19
1

No jQuery required :P

<script type="text/javascript">

    //var subject = '3,4,9';
    //var subject = '3,,9';
    var subject = ',,4,9';

    var clean = Array();
    var i = 0;

    subject = subject.split(',');

    for (var a in subject)
    {
        if(subject[a].length)
        {
            clean[i] = subject[a];
            i++;
        }
    }

    document.write(clean.join(','));

</script>
Kirk Strobeck
  • 17,984
  • 20
  • 75
  • 114
1

You may also use pure javascript. Let say you want to take off only "4":

value = value.replace(/4,?/, '')

or "3" and "9":

value = value.replace(/([39],?)+/, '')
Yuri Ghensev
  • 2,507
  • 4
  • 28
  • 45
0

I think this function will work for what you are trying to do: http://www.w3schools.com/jsref/jsref_split.asp

string.split(separator, limit)
thekoehl
  • 66
  • 5
0

use

array = string.split(separator);

to break a string into an array. then use this to join after manipulations.

string = array.join(separator);
dqhendricks
  • 19,030
  • 11
  • 50
  • 83
0
var ary = value.split(',');
ary.splice(indexOfItemToRemove,1)
var result = ary.join(',');
Keith
  • 5,311
  • 3
  • 34
  • 50
0

This is discussed in another post:

remove value from comma separated values string

var removeValue = function(list, value, separator) {
   separator = separator || ",";
   var values = list.split(",");
   for(var i = 0 ; i < values.length ; i++) {
      if(values[i] == value) {
         values.splice(i, 1);
         return values.join(",");
      }
   }
   return list;
}
Community
  • 1
  • 1
Joe Hanink
  • 4,767
  • 4
  • 19
  • 21
0

You can use this function:

function removeComma(x) {
    var str = '';
    var subs = '';

    for(i=1; i<=x.length; i++) {
        subs = x.substring(i-1, i).trim();

        if(subs !== ',') {
            str = str+subs;
        }
    }

    return str;
}
Anh Pham
  • 2,108
  • 9
  • 18
  • 29