0

When I use the below script in Wordpress I get a error:

TypeError: document.querySelector(...) is null

<script type="text/javascript" >
    document.querySelector('input[list]').addEventListener('input', function(e) {
        var input = e.target,
            list = input.getAttribute('list'),
            options = document.querySelectorAll('#' + list + ' option'),
            hiddenInput = document.getElementById(input.id + '-hidden'),
            inputValue = input.value;

        hiddenInput.value = inputValue;

        for(var i = 0; i < options.length; i++) {
            var option = options[i];

            if(option.innerText === inputValue) {
                hiddenInput.value = option.getAttribute('data-value');
                break;
            }
        }
    });
</script>

How can I resolve this? I'm trying to get the id of an option that I select in a datalist.

<input list="suggestionList" id="answerInput">
<datalist id="suggestionList">
    <option data-value="42">The answer</option>
</datalist>
<input type="hidden" name="answer" id="answerInput-hidden">
Rory McCrossan
  • 306,214
  • 37
  • 269
  • 303
Puya Sarmidani
  • 1,000
  • 6
  • 25

0 Answers0