Jump to content

Replace with regular expression

Recommended Posts

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);
		var taggedPerson = $(e).data('id')

		var orig = $('#peopleLinkedToArticle').val()
		var t = new RegExp(taggedPerson, 'g')
		var newval = orig.replace(t,'')

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

Link to post
Share on other sites

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).

Link to post
Share on other sites

Join the conversation

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

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.