question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is it possible to clear the input programatically

See original GitHub issue

I would like to clear the input box programmatically. I’ve almost made it work, but the result is the dropdown opens again ad a side effect.

clear

I’m doing this:

   accessibleAutocomplete({
      element: containerElement,
      selectElement: multiselectElement,
      defaultValue: '',
      confirmOnBlur: false,
      showAllValues: true,
      id: autocompleteId,
      onConfirm: handleAdd,
      source: getSource
    });
    function handleAdd(value) {
      // hack to clear the input box. delay 150ms to allow the react component
      // to render iwth the new value first.
      // hide the selected value by making it the same colour as the input box
      autocompleteInputElement.style.color = 'white';
      setTimeout(function() {
        autocompleteInputElement.value = '';
        autocompleteInputElement.style.color = 'black';
      }, 150);
    }

yes., it’s a horrible hack and is subject to race condition. is there a built-in way?

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:3
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
edwardhorsfordcommented, Oct 7, 2020

I got this working fully today (I hope!). Code here.

Edward, your link is dead. Do you have this code available somewhere else?

Fixed. The source repo had a reorg, so repointed to the new location.

1reaction
edwardhorsfordcommented, Oct 7, 2020

I got this working fully today (I hope!). Code here.

Required this dance to get it to work:

const removeValue = () => {
        // Clear autocomplete and hidden select
        $enhancedElement.val('')
        $(element).parent().find('select').val('')

        // Needed to collapse menu
        $enhancedElement.click()
        $enhancedElement.focus()
        $enhancedElement.blur()

        // Return focus to the button
        removeButton.focus()
      }

How it looks on our service: Screenshot 2020-04-29 at 18 32 10

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is there a way to programmatically clear an input's value ...
One option is to include a Boolean, called cleared , for example, in your model. If cleared is True, in your view function...
Read more >
HTML | Clearing the input field - GeeksforGeeks
To clear the entire input field without having to delete the whole thing manually Or what if there is already a pre-suggested input...
Read more >
Clear form input programmatically - Framework7 Forum
I want to simulate the 'input-clear-button' programmatically - F7 uses the function clearInput but when I call that I get an error Cant...
Read more >
How to Clear Input Fields in JavaScript - Linux Hint
To clear input fields in JavaScript, use the onfocus event and target property,onclick event and document.getElementById() method or the reset() method.
Read more >
How to Clear Input Values of Dynamic Form in React
How to clear the values all inputs. At the top of handleReset , use document.querySelectorAll('input') to select all the input elements on the ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found