Jump to content

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

Use the split() string method to convert the string into an array, and then use splice() to remove the item specified. Remember that array indexes start from 0. You can convert the array back to a string using join().

Edited by Adam

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.
 */
Edited by Xaotique
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.