Adamhumbug Posted August 20, 2020 Share Posted August 20, 2020 Hi All, I have a number of buttons that when clicked, add their id to a hidden input with a comma. I would like the ability to click another button and remove its id from this input. I have the following that works well function removeTaggedPeople(){ var e = event.target var tagButton = $(e); $(e).attr('onclick','tagInArticle()') var taggedPerson = $(e).data('id') var orig = $('#peopleLinkedToArticle').val() var t = new RegExp(taggedPerson, 'g') var newval = orig.replace(t,'') $('#peopleLinkedToArticle').val(newval) $(e).remove() } The issue is that if i have the id "1" and the id "11" in the input and i click the button with id "1" it will remove every instance of the number 1 in the string. I would like it to only remove "1" Im not sure if you need a little more to go on here but thats my predicament. Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/311355-replace-with-regular-expression/ Share on other sites More sharing options...
NotSunfighter Posted August 20, 2020 Share Posted August 20, 2020 Off the top of my head and not testing the idea; I would just add a comma to the 'var taggedPerson'. Quote Link to comment https://forums.phpfreaks.com/topic/311355-replace-with-regular-expression/#findComment-1580794 Share on other sites More sharing options...
Adamhumbug Posted August 20, 2020 Author Share Posted August 20, 2020 6 minutes ago, NotSunfighter said: Off the top of my head and not testing the idea; I would just add a comma to the 'var taggedPerson'. That works sometimes, if it looking to remove 1, it will also remove one of the 1's from 11, Quote Link to comment https://forums.phpfreaks.com/topic/311355-replace-with-regular-expression/#findComment-1580795 Share on other sites More sharing options...
Barand Posted August 20, 2020 Share Posted August 20, 2020 Find a better way to store the ids than a comma-separated list. 1 Quote Link to comment https://forums.phpfreaks.com/topic/311355-replace-with-regular-expression/#findComment-1580804 Share on other sites More sharing options...
maxxd Posted August 21, 2020 Share Posted August 21, 2020 If you're doing it all in JavaScript, store the values as an array then stringify the array when the form is submitted (assuming you're doing a full page refresh on submit; if you're using AJAX just send the array). Quote Link to comment https://forums.phpfreaks.com/topic/311355-replace-with-regular-expression/#findComment-1580821 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.