Textarea in checkbox

Hi!

If I write the word “car” in the textarea, then checkbox is ON. Remove car then checkbox is empty. Is possible?

Paste must also work like
Car
Bus

<body>
                <form>
                    <textarea name="type" rows="5" cols="35"></textarea><br>
                    <input type="checkbox" value="Car"><label>Car</label><br>
                    <input type="checkbox" value="Bus"><label>Bus</label><br>
                    <input type="checkbox" value="3"><label>3</label><br>
                    <input type="checkbox" value="4"><label>4</label>
                </form>
            </body>

Like this but works the opposite:

Use a OnChange event on the text area. Loop through your checkboxes and use contains or indexOf on the text area to check whether the value of the checkbox is in the text area

pseudocode

foreach checkbox {
  if (textarea.value.indexOf(checkbox.value) > -1) {
     check checkbox
  } 
}

My .js is very bad :stuck_out_tongue:

Is this the final code or does it need to add something? :see_no_evil:

No, it’s not the final but there should be enough information for you to take a shot at trying it. If you make the attempt, it often leads you to being able to solve other issues later.

So you need to find:

  1. How to fire the onChange event on the text area
  2. How to generate the collection of checkboxes
  3. Once you have the collection, loop through it to check the value of each against the text area (the pseudo-code is VERY close - you will need to change the textarea to the appropriate object and the checkbox
  4. Then you’ll need to be able to set that appropriate checkbox.

You should be able to leverage some of the code you had before from your other thread

2 Likes

This is code and demo: https://jsfiddle.net/ck8uj492/

How to use your code. I got only error if edit code :see_no_evil:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.