# Replace single value from text list field Using JS to Bubble

**URL:** <https://forum.bubble.io/t/replace-single-value-from-text-list-field-using-js-to-bubble/352266>\
**Category:** Tips\
**Created:** [December 16, 2024, 7:03am UTC](https://forum.bubble.io/t/replace-single-value-from-text-list-field-using-js-to-bubble/352266 "2024-12-16T07:03:19Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![boston85719](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/boston85719/32/77286_2.png) [@boston85719](https://forum.bubble.io/u/boston85719)\
**Post date:** [December 16, 2024, 7:03am UTC](https://forum.bubble.io/t/replace-single-value-from-text-list-field-using-js-to-bubble/352266/1 "2024-12-16T07:03:19Z")

</div>

I am building a function to have a data structure that is a list of text. When attempting to make changes to the list of text, I ran into a poor UX, which is that it is not possible to change a particular text value in the list and keep the list in the same order.

I used the toolbox plugin and JS to Bubble element to make it possible using the code below

```auto
function replaceValueInList(originalList, targetValue, newValue) {
    // Ensure the input is an array or a string that can be converted to an array
    if (typeof originalList === "string") {
        // If the input is a comma-separated string, convert it into an array
        originalList = originalList.split(",").map(item => item.trim());
    }

    if (!Array.isArray(originalList)) {
        console.error("The provided list is not a valid array or a comma-separated string.");
        return;
    }

    // Replace the target value with the new value while keeping the order
    const updatedList = originalList.map(value => value === targetValue ? newValue : value);

    // Convert the updated array into a JSON string
    const updatedListJSON = JSON.stringify(updatedList);

    // Send the updated list as a JSON string back to Bubble
    if (typeof bubble_fn_updatedList === "function") {
        bubble_fn_updatedList(updatedListJSON);
    } else {
        console.error("JavaScript to Bubble function 'bubble_fn_updatedList' is not defined.");
    }
}

// Example usage (replace with dynamic data from Bubble):
const originalList = "123, 456, 789"; // Input list as a comma-separated string
const targetValue = "456"; // Value to replace
const newValue = "555"; // New value

// Call the function
replaceValueInList(originalList, targetValue, newValue);

```

Note, on the JS to Bubble I am using the value as a single value and not a list

 ![Screen Shot 2024-12-16 at 1.54.55 PM](https://us1.discourse-cdn.com/bubble/original/3X/5/5/550ecf9ca237f8b24474bff96fab6211e9a9fd3c.png)

The dynamic values in the javascript from above

 ![Screen Shot 2024-12-16 at 1.55.30 PM](https://us1.discourse-cdn.com/bubble/original/3X/b/c/bc8af59ded8ea892ee02ab79f8325b40b2a07492.png)  
 ![Screen Shot 2024-12-16 at 1.55.18 PM](https://us1.discourse-cdn.com/bubble/original/3X/8/c/8c544f7e1d065d1c4eb46db34cc9f225c746e656.png)

Regex pattern: `"([^"]+)"`  
find and replace is just finding the quotation mark and replace value is empty

 ![Screen Shot 2024-12-16 at 1.57.31 PM](https://us1.discourse-cdn.com/bubble/original/3X/f/4/f40517fef001747062ce224029c3ec3612c71197.png)

need the join with operator as a comma and space to create the list of text to add into DB

![examplechangetextlist-ezgif.com-video-to-gif-converter](https://us1.discourse-cdn.com/bubble/original/3X/3/3/33d27b3fdcf4ec56c64a09b03c82b50ea4cf0ad7.gif)

---

<div class="post-metadata">

**Author:** ![chris.williamson1996](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/chris.williamson1996/32/70581_2.png) [@chris.williamson1996](https://forum.bubble.io/u/chris.williamson1996)\
**Post date:** [December 16, 2024, 7:47am UTC](https://forum.bubble.io/t/replace-single-value-from-text-list-field-using-js-to-bubble/352266/2 "2024-12-16T07:47:19Z")

</div>

Sweet solution! I’ve not messed with this exact problem but I’m curious if you do this if you can achieve it in pure bubble.

Below Parent group just refers to the host data type the list of text is in.

List = Set list = parent group list:converted to text:find and replace (old value you want to change with new value):join with (,)

Essentially taking the list from a list to a text string, replacing the values and keeping order, then reformatting it into a list again.

On the converted to text you may need to add :formatted as text  
(this text),

I’m not at a computer this is just off top of my head so please excuse the rough concept 😅

@boston85719

---

<div class="post-metadata">

**Author:** ![boston85719](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/boston85719/32/77286_2.png) [@boston85719](https://forum.bubble.io/u/boston85719)\
**Post date:** [December 16, 2024, 8:40am UTC](https://forum.bubble.io/t/replace-single-value-from-text-list-field-using-js-to-bubble/352266/3 "2024-12-16T08:40:30Z")

</div>

> [@chris.williamson1996](#):
>
> List = Set list = parent group list:converted to text:find and replace (old value you want to change with new value):join with (,)

I tried this…basically if my list has values such as 123, 456, 123456, 789, 123456789 and I attempt to change the value of 123, then the values of 123456 and 123456789 also get changed because the :find and replace will take the original value of 123 and replace that with the replacement value in all values in the list of text that contain the original value, which means, changing 123 to 321 will also change 123456 to 321456 and 123456789 to 321456789

---

<div class="post-metadata">

**Author:** ![chris.williamson1996](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/chris.williamson1996/32/70581_2.png) [@chris.williamson1996](https://forum.bubble.io/u/chris.williamson1996)\
**Post date:** [December 16, 2024, 9:08am UTC](https://forum.bubble.io/t/replace-single-value-from-text-list-field-using-js-to-bubble/352266/4 "2024-12-16T09:08:04Z")

</div>

Very could point, did not consider this scenario 🙌

---

<div class="post-metadata">

**Author:** ![ihsanzainal84](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/ihsanzainal84/32/245932_2.png) [@ihsanzainal84](https://forum.bubble.io/u/ihsanzainal84)\
**Post date:** [December 16, 2024, 12:45pm UTC](https://forum.bubble.io/t/replace-single-value-from-text-list-field-using-js-to-bubble/352266/5 "2024-12-16T12:45:33Z")

</div>

Try using a Regex expression in the find and replace.

I have something similar set up for my pseudo JS values. ChatGPT is great at generating regex expressions.

---

<div class="post-metadata">

**Author:** ![boston85719](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/boston85719/32/77286_2.png) [@boston85719](https://forum.bubble.io/u/boston85719)\
**Post date:** [December 16, 2024, 2:47pm UTC](https://forum.bubble.io/t/replace-single-value-from-text-list-field-using-js-to-bubble/352266/6 "2024-12-16T14:47:59Z")

</div>

It’s all set up and working. Do you have a regex expression that would do this?

---

<div class="post-metadata">

**Author:** ![bubble.trouble](https://avatars.discourse-cdn.com/v4/letter/b/7ba0ec/32.png) [@bubble.trouble](https://forum.bubble.io/u/bubble.trouble)\
**Post date:** [December 16, 2024, 4:12pm UTC](https://forum.bubble.io/t/replace-single-value-from-text-list-field-using-js-to-bubble/352266/7 "2024-12-16T16:12:56Z")

</div>

@boston85719 the whole point of this exercise being to save a repeating list group items at the same time correct?

---

<div class="post-metadata">

**Author:** ![rpetribu](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/rpetribu/32/90852_2.png) [@rpetribu](https://forum.bubble.io/u/rpetribu)\
**Post date:** [December 16, 2024, 5:33pm UTC](https://forum.bubble.io/t/replace-single-value-from-text-list-field-using-js-to-bubble/352266/8 "2024-12-16T17:33:09Z")

</div>

Couldn’t handle myself!  
Did in just 3 steps with pure Bubble.  
See if works for you.

> **[Bubble - Visual Programming](https://internal-test-ez.bubbleapps.io/version-test)**
>
> Build stuff without code and launch a startup without a tech-cofounder! Bubble is a visual programing language. Instead of typing code, use a visual editor to build applications.

Editor: [Here](https://bubble.io/page?id=internal-test-ez&tab=Design&name=index)

---

<div class="post-metadata">

**Author:** ![Bluebird](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/bluebird/32/237882_2.png) [@Bluebird](https://forum.bubble.io/u/Bluebird)\
**Post date:** [December 17, 2024, 1:00am UTC](https://forum.bubble.io/t/replace-single-value-from-text-list-field-using-js-to-bubble/352266/9 "2024-12-17T01:00:52Z")

</div>

is there a good reason why you’re using a single text and the split by operator instead of an actual list of text? why not set “value is a list” to true in the js to bubble plugin. then ask chatgpt to rewrite this same function, but to accept an array. just place 2 sqaure brackets, and between them, a dynamic expression (this assumes that the updated list’s value is also an array now): `Js Edit Container ID Updated List's values each item: formatted as json safe`. now, js to bubble spits out an array that you save to the database. i see that in your use case, you only accept numbers, but in general, it’s better to use formatted as json safe in the old and new value in the js too instead of writing the quotes yourself

---

<div class="post-metadata">

**Author:** ![boston85719](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/boston85719/32/77286_2.png) [@boston85719](https://forum.bubble.io/u/boston85719)\
**Post date:** [December 17, 2024, 6:22am UTC](https://forum.bubble.io/t/replace-single-value-from-text-list-field-using-js-to-bubble/352266/10 "2024-12-17T06:22:39Z")

</div>

> [@rpetribu](#):
>
> Did in just 3 steps with pure Bubble.

 ![Screen Shot 2024-12-17 at 1.21.03 PM](https://us1.discourse-cdn.com/bubble/original/3X/a/1/a1f2700682a97ce8e02891bbde2390e43a3fc9bd.png)

This is what I see on the link you provided…it seems to use 5 steps and the Ultimate Toolkit plugin…Do you have a version that uses pure Bubble and does it in 3 steps?

---

<div class="post-metadata">

**Author:** ![boston85719](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/boston85719/32/77286_2.png) [@boston85719](https://forum.bubble.io/u/boston85719)\
**Post date:** [December 17, 2024, 6:25am UTC](https://forum.bubble.io/t/replace-single-value-from-text-list-field-using-js-to-bubble/352266/11 "2024-12-17T06:25:54Z")

</div>

> [@Bluebird](#):
>
> i see that in your use case, you only accept numbers

The example was just numbers, the setup works for any combination of letters, numbers and special characters

> [@Bluebird](#):
>
> is there a good reason why you’re using a single text and the split by operator instead of an actual list of text? why not set “value is a list” to true in the js to bubble plugin. then ask chatgpt to rewrite this same function, but to accept an array. just place 2 sqaure brackets, and between them, a dynamic expression (this assumes that the updated list’s value is also an array now):

Couldn’t get chatgpt to do that successfully…if you can take the code and have chatgpt alter it to work with js to bubble as a list please share here the code. I tried myself for more time than I was willing to waste on it, while the setup I have works just fine, just requires me to remove the square brackets and the quotes.

---

<div class="post-metadata">

**Author:** ![boston85719](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/boston85719/32/77286_2.png) [@boston85719](https://forum.bubble.io/u/boston85719)\
**Post date:** [December 17, 2024, 6:27am UTC](https://forum.bubble.io/t/replace-single-value-from-text-list-field-using-js-to-bubble/352266/12 "2024-12-17T06:27:10Z")

</div>

> [@bubble.trouble](#):
>
> the whole point of this exercise being to save a repeating list group items at the same time correct?

No, it is to change a single value in a list of texts (ie: a data field that is of type text and is a list) and not have the list of texts order change.

> [@boston85719](#):
>
> When attempting to make changes to the list of text, I ran into a poor UX, which is that it is not possible to change a particular text value in the list and keep the list in the same order.

---

<div class="post-metadata">

**Author:** ![adamhholmes](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/adamhholmes/32/100510_2.png) [@adamhholmes](https://forum.bubble.io/u/adamhholmes)\
**Post date:** [December 17, 2024, 9:26am UTC](https://forum.bubble.io/t/replace-single-value-from-text-list-field-using-js-to-bubble/352266/13 "2024-12-17T09:26:51Z")

</div>

> [@Bluebird](#):
>
> is there a good reason why you’re using a single text and the split by operator instead of an actual list of text? why not set “value is a list” to true in the js to bubble plugin.

Yeah…

Surely it’s easier, and more efficient, to use an array here…

Then just find the index of the item (using `findIndex()`) and reassign the value for that item.

---

<div class="post-metadata">

**Author:** ![ihsanzainal84](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/ihsanzainal84/32/245932_2.png) [@ihsanzainal84](https://forum.bubble.io/u/ihsanzainal84)\
**Post date:** [December 17, 2024, 9:40am UTC](https://forum.bubble.io/t/replace-single-value-from-text-list-field-using-js-to-bubble/352266/14 "2024-12-17T09:40:13Z")

</div>

Here’s a demo: [Demo](https://coffeehat-sandbox.bubbleapps.io/version-test/demo-regex_on_list?debug_mode=true)

[Editor](https://bubble.io/page?id=coffeehat-sandbox&tab=Workflow&name=demo-regex_on_list&type=page&wf_item=bTIUv)

Here’s the regex I got  
`(?<=^|\|\|)\s*(daynamic_value)\s*(?=\|\||$)`

I use `||` as a delimiter in the demo as that’s my usual practice for this sort of things.

ChatGPT does have a little issue with Bubble regex but you can guide it with this [Bubble regex Doc](https://manual.bubble.io/core-resources/data/operations-and-comparisons#extract-with-regex).

---

<div class="post-metadata">

**Author:** ![rpetribu](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/rpetribu/32/90852_2.png) [@rpetribu](https://forum.bubble.io/u/rpetribu)\
**Post date:** [December 17, 2024, 10:23am UTC](https://forum.bubble.io/t/replace-single-value-from-text-list-field-using-js-to-bubble/352266/15 "2024-12-17T10:23:41Z")

</div>

There are 5 steps so you can see what I am caching. The toolbox is just for this purpose. You can simply save the variables in some custom state… no big deal. Nothing hard to do… as I said, pure Bubble.

---

<div class="post-metadata">

**Author:** ![boston85719](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/boston85719/32/77286_2.png) [@boston85719](https://forum.bubble.io/u/boston85719)\
**Post date:** [December 17, 2024, 11:16am UTC](https://forum.bubble.io/t/replace-single-value-from-text-list-field-using-js-to-bubble/352266/16 "2024-12-17T11:16:07Z")

</div>

Thanks for that…this works great. Now two approaches in the toolbelt

> [@adamhholmes](#):
>
> Surely it’s easier, and more efficient, to use an array here…
> 
> Then just find the index of the item (using `findIndex()`) and reassign the value for that item.

How is this done? Would be great to get another working example on this tip thread

---

<div class="post-metadata">

**Author:** ![adamhholmes](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/adamhholmes/32/100510_2.png) [@adamhholmes](https://forum.bubble.io/u/adamhholmes)\
**Post date:** [December 17, 2024, 11:54am UTC](https://forum.bubble.io/t/replace-single-value-from-text-list-field-using-js-to-bubble/352266/17 "2024-12-17T11:54:23Z")

</div>

Assuming you have a list of texts: (e.g.)

```auto
let myList = [
  'item 1',
  'item 2',
  'item 3',
  'item 4'
]

```

etc.

Then you can use the following:

```auto
function updateList(originalValue, newValue) {
  let index = myList.findIndex(item => item === originalValue);
  myList[index] = newValue;
}

```

And then invoke the function, with the intended parameters e.g.

`updateList('item 2', 'item 406');`

To change ‘item 2’ to ‘item 406’

you probably also want to include a check to make sure the item to change actually exists, to avoid any potential issues… such as:

```auto
function updateList(originalValue, newValue) {
  let index = myList.findIndex(item => item === originalValue);
  if (index >= 0) {
    myList[index] = newValue;
  } else {
    console.log(`Item ${originalValue} not found`);
  }
}

```

---

<div class="post-metadata">

**Author:** ![bubble.trouble](https://avatars.discourse-cdn.com/v4/letter/b/7ba0ec/32.png) [@bubble.trouble](https://forum.bubble.io/u/bubble.trouble)\
**Post date:** [December 17, 2024, 2:22pm UTC](https://forum.bubble.io/t/replace-single-value-from-text-list-field-using-js-to-bubble/352266/18 "2024-12-17T14:22:09Z")

</div>

This kind of “programming” is really not the best way. If it works for your scenario that is great. But just for future travelers, findIndex will only return the first found item. If your list can contain duplicate values (e.g. 123, 234,345, 123,457), and the user tried to replace the second “123” in this scenario, the first 123 would actually be replaced and the second would be left as is.

Based on the fact that the OP has a list of ID’s I would assume these will all be unique, but what if the user accidentally types 123 twice not realizing it and then tries to correct it later? It could lead to a confusing experience.

---

<div class="post-metadata">

**Author:** ![boston85719](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/boston85719/32/77286_2.png) [@boston85719](https://forum.bubble.io/u/boston85719)\
**Post date:** [December 17, 2024, 2:28pm UTC](https://forum.bubble.io/t/replace-single-value-from-text-list-field-using-js-to-bubble/352266/19 "2024-12-17T14:28:39Z")

</div>

> [@bubble.trouble](#):
>
> If your list can contain duplicate values

If you use a field that is a list in bubble it is not possible to have duplicates

> [@bubble.trouble](#):
>
> what if the user accidentally types 123 twice not realizing it and then tries to correct it later? It could lead to a confusing experience.

In my setup, the user is able to see all the IDs they have recently added and are already stored in the database, in the event an existing item in the list is the same as a newly added item (added not to db but in the RG) they see both and can alter it if they spot it, but since it saves to a field, the duplicate doesn’t actually create a duplicate.

BTW, @bubble.trouble do you have an alternative approach that also works, that doesn’t require ‘programming’…so far best solution in this thread is from @ihsanzainal84 with the use of the regex expression, and I personally plan to return to this thread to utilize that approach in the future if I need the functionality again. But at the end of the day, the more workable solutions there are, the better as some may be better suited for different use cases.

---

<div class="post-metadata">

**Author:** ![adamhholmes](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/adamhholmes/32/100510_2.png) [@adamhholmes](https://forum.bubble.io/u/adamhholmes)\
**Post date:** [December 17, 2024, 2:42pm UTC](https://forum.bubble.io/t/replace-single-value-from-text-list-field-using-js-to-bubble/352266/20 "2024-12-17T14:42:43Z")

</div>

> [@bubble.trouble](#):
>
> This kind of “programming” is really not the best way.

I’m not sure what that’s supposed to mean,…

And you haven’t offered any ‘better’ solution here…

findIndex() returns the index of the first item matching the condition - which is EXACTLY what’s needed here.

_why_ would there be more than one item in the list with the same ID (although, personally I always use object arrays for this, not strings) - and what would the intended behaviour be if there _were_ duplicate IDs in the list?

In any case, that’s an entirely different question - but obviously if you need to change Multiple items then a different method needs to be used.

If you do this right, in my opinion/experience it _really_ is the best way to do it (much simpler, cleaner, and more efficient than any alternatives I’ve seen or tried).

But I’d be interested to hear your ‘better’ way…

[Next page](https://forum.bubble.io/t/replace-single-value-from-text-list-field-using-js-to-bubble/352266.md?page=2)
