2
I have these two codes below, where Javascript does not allow enable the attribute required
and I’ve tried many forms, but they have no effect. Someone has a solution for this?
/**
* Captuar o elemento responsável por
* capturar o valor digitado pelo usuário
*/
const inputSearch = document.querySelector('input[name="q"]')
/**
* Aplica um evento, do tipo "click", nos botões de pesquisa.
* Para evitar incompatibilidades, você pode substituir o `forEach`
* por um `for` "simples"
*/
document.querySelectorAll("#google-search, #wikipedia-search, #yahoo-search, #bing-search, #yandex-search, #duckduckgo, #archive-search, #ask-search, #aol-search, #Baidu-search, #infopedia-search, #wolframalpha-search, #cambio-search, #tempo-search, #dicionariopenal-search, #interpretacaocpp-search, #interpretacaocp-search")
.forEach(btn => btn.addEventListener('click', search))
/**
* Função responsável por identificar o botão pressionado,
* e enviar o usuário para o site correto.
*
* @param {EventTarget} event
* @return void
*/
function search(event) {
event.preventDefault()
const anchor = document.createElement('a')
anchor.target = "_self"
anchor.href = `${event.target.getAttribute('formaction')}${encodeURI(inputSearch.value)}`
anchor.click()
}
<input type="text" class="col-sm-7" style="width:52%;height:44px;border: 1px solid #ebebeb;position: relative;top: 3px;background-color:#ffffff;" id="searchresultsquery" placeholder="Escreva o número do artigo do CPP e faça enter..." autofocus name="q" size="70" maxlength="70" required/>
<input type="image" id="dicionariopenal-search" style="width:45px;position: relative;left:0px;top:19px;" src="https://www.clubesorte.pt/corampopulo/c_elements/images/fotos/search-image1.png" alt="Submit Form" formaction="https://www.clubesorte.pt/corampopulo/login/interpretacao-cpp/palavras.php?letter=artigo ">
</form>
required
only works with form Ubmit.– Augusto Vasques
Meireles, besides the information provided by Augusto, you have actually used the required attribute incorrectly: simply declare (it’s like a Boolean), see an example here: https://www.w3schools.com/tags/att_input_required.asp
– Daniel Mendes