Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

94
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!