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

97
Visualizações
Can't find all Elements with JQuery

In this assignment, the HTML page is given to me and I can't make changes to the HTML page. My task is to add an image into the website by using JQuery. The code is as given for the HTML page:

 <!-- Sign Up  -->
  <template id="signUp">
    <form @submit.prevent='onSubmit' ref='form' action="" class='register-form'>
      <h2>New Account</h2>
      ...
      <input type="submit" :disabled='!isFormValid' value='Register'>
    </form>
  </template>

  <!-- Sign In  -->
  <template id="signinIn">
    <form ref='form' @submit.prevent='handleForm' action="" class='signin-form'>
      <h2>Sign In</h2>
      ...
      <input :disabled='!isFormValid' type="submit" value="Sign In">
    </form>
  </template>

The assignment want us to add an image above both the New Account and Sign In h2 tag by using JQuery. I code is as followed:

$(document).ready(function () {
  console.log("Document ready");

  const $form = $("form");
  const $body = $("body");
  const $wrapper = $("<div class='wrapper flex-column'>");

  // function to add logo above form
  const addImage = (form) => {
    form.find("h2").prepend(`
      <div class='logo flex-row'>
        <img src='./assets/image.png' alt="logo" />
      </div>
    `);
  };

  // wrap body contents
  $body.wrapInner($wrapper);

  addImage($form);
});

The idea is to add the image above both the h2 tag from sign up and sign in. The problem I'm running into is that I'm only getting one image above the New Account h2 tag and not for both. Any suggestions?

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

0

You can reduce all your jQuery code to the following:

$( "form h2" ).before("<div>hi</div>");

Then substitute the code you want for your image where I put the div for saying "hi".

This works by using the magic of the following:

jQuery selectors: https://www.w3schools.com/jquery/jquery_selectors.asp

jQuery before() method: https://api.jquery.com/before/

For the selector, you're saying "give me all h2 elements that are within a form element". Then just execute the before() method on that returned list.

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