Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

126
Visualizações
javascript/jquery only affects first element

I have this html code for a search form and i would like to use jquery to edit the url to

let searchform, searchurl, i; searchurl = window.location.href;

searchform = document.querySelector("form.dgwt-wcas-search-form");
searchform.action = searchform.action + "index.php";
<form class="dgwt-wcas-search-form" role="search" action="https://www.myurl.com/" method="get">
        <div class="dgwt-wcas-sf-wrapp">
                        <label class="screen-reader-text" for="dgwt-wcas-search-input-3">Products search</label>
            <input id="dgwt-wcas-search-input-3" type="search" class="dgwt-wcas-search-input" name="s" value="" placeholder="Search for products..." autocomplete="off">
            <div class="dgwt-wcas-preloader" style="right: 81.2812px;"></div>
                            <button type="submit" aria-label="Search" class="dgwt-wcas-search-submit">Search</button>
            <input type="hidden" name="post_type" value="product">
            <input type="hidden" name="dgwt_wcas" value="1">
</div>
</form>

<form class="dgwt-wcas-search-form" role="search" action="https://www.myurl.com/" method="get">
        <div class="dgwt-wcas-sf-wrapp">
                        <label class="screen-reader-text" for="dgwt-wcas-search-input-3">Products search</label>
            <input id="dgwt-wcas-search-input-3" type="search" class="dgwt-wcas-search-input" name="s" value="" placeholder="Search for products..." autocomplete="off">
            <div class="dgwt-wcas-preloader" style="right: 81.2812px;"></div>
                            <button type="submit" aria-label="Search" class="dgwt-wcas-search-submit">Search</button>       
            <input type="hidden" name="post_type" value="product">
            <input type="hidden" name="dgwt_wcas" value="1">
                    </div>
    </form>

The script only changes the first url in searchbox into https://www.myurl.com/index.php but the second searchbox will not get the script to run.

I would like two search forms to change the url from https://www.myurl.com/ to https://www.myurl.com/index.php

Any help would be greatly appreciated.

Thank you

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

It's because you're using a wrong document function to query, you should have used "querySelectorAll" instead of "querySelector" so that it returns all items that match the query.

After that, you'll need to loop over the array of forms and set action for each form just like you're doing.

about 4 years ago · Juan Pablo Isaza Relatório

0

let searchform, searchurl, i;
searchurl = window.location.href;

searchform = document.querySelector("form.dgwt-wcas-search-form");
searchform.action = searchform.action + "index.php";
<form class="dgwt-wcas-search-form" role="search" action="https://www.myurl.com/" method="get">
  <div class="dgwt-wcas-sf-wrapp">
    <label class="screen-reader-text" for="dgwt-wcas-search-input-3">Products search</label>
    <input id="dgwt-wcas-search-input-3" type="search" class="dgwt-wcas-search-input" name="s" value="" placeholder="Search for products..." autocomplete="off">
    <div class="dgwt-wcas-preloader" style="right: 81.2812px;"></div>
    <button type="submit" aria-label="Search" class="dgwt-wcas-search-submit">Search</button>
    <input type="hidden" name="post_type" value="product">
    <input type="hidden" name="dgwt_wcas" value="1">
  </div>
</form>

<form class="dgwt-wcas-search-form" role="search" action="https://www.myurl.com/" method="get">
  <div class="dgwt-wcas-sf-wrapp">
    <label class="screen-reader-text" for="dgwt-wcas-search-input-3">Products search</label>
    <input id="dgwt-wcas-search-input-3" type="search" class="dgwt-wcas-search-input" name="s" value="" placeholder="Search for products..." autocomplete="off">
    <div class="dgwt-wcas-preloader" style="right: 81.2812px;"></div>
    <button type="submit" aria-label="Search" class="dgwt-wcas-search-submit">Search</button>
    <input type="hidden" name="post_type" value="product">
    <input type="hidden" name="dgwt_wcas" value="1">
  </div>
</form>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda