The following JavaScript code to set focus on the text input works in Codpen, but not on my Client's website (in development). No errors are displayed and I can use classList
to add or remove classes for the same text input.
Not sure where the issue is in my code...
var searchToggle = document.querySelectorAll( ".js--search-toggle" );
const searchForm = document.querySelector( ".header-search-form" );
const searchInput = document.querySelector( ".search-field" );
searchToggle.forEach(item => {
item.addEventListener('click', e => {
searchForm.classList.toggle( "open" );
searchInput.style.backgroundColor = "red"; // This works
searchInput.focus(); // This does not
});
});
<form role="search" method="get" class="header-search-form" action="{{ site.url }}">
<label for=""></label>
<input type="search" class="search-field" autocomplete="off" placeholder="Search for a topic" value="" name="s" />
<button type="button" class="btn js--search-toggle">
Set Search Focus
</button>
</form>
Update