Jump to content

Removing comma seperated value


dweb

Recommended Posts

hi all

 

i have a input field on my webpage that looks like

<input name="people" id="people" value="1,4,6,8,10,23">

and I want to run a javascript so when my link, such as

<a href='javascript:remove_comma(3)'>click</a>

is clicked, it would remove the 3rd comma separated value, leaving me with

<input name="people" id="people" value="1,4,8,10,23">

but i might also do

<a href='javascript:remove_comma(1)'>click</a>

<a href='javascript:remove_comma(5)'>click</a>

can anyone help?

 

thanks

Link to comment
https://forums.phpfreaks.com/topic/279385-removing-comma-seperated-value/
Share on other sites

Just to give an example of what Adam said.

JSFiddle: http://jsfiddle.net/gfFG9/

function removeCSV(elm, pos)
{
    var arr = elm.value.split(',');
    arr.splice(pos, 1);
    
    elm.value = arr.join(',');
}

var element = window.document.querySelector("#people");
removeCSV(element, 0); // Remove the first value.

/*
 * 0, 1, 2, 3 .. for elements.  Element 1 is the second, 2
 * the third, and so on.
 */

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.