dweb Posted June 20, 2013 Share Posted June 20, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/279385-removing-comma-seperated-value/ Share on other sites More sharing options...
Adam Posted June 20, 2013 Share Posted June 20, 2013 (edited) 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 June 20, 2013 by Adam Quote Link to comment https://forums.phpfreaks.com/topic/279385-removing-comma-seperated-value/#findComment-1437056 Share on other sites More sharing options...
codefossa Posted June 20, 2013 Share Posted June 20, 2013 (edited) 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 June 20, 2013 by Xaotique Quote Link to comment https://forums.phpfreaks.com/topic/279385-removing-comma-seperated-value/#findComment-1437072 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.