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

146
Views
InnerHTML doesn't work with if condition in regExp

i'm trying to add some confirmation to my form using RegExp The thing is that i use some condition in that way i can show to the user that the input is correct or not So i've created a below my input and using nextElementSibling and after that i'm using a innerHtml to add some Text and show it to the user here's the project on my GitHub https://github.com/Chardema/openclassroomp3.github.io But that doesn't work The is correct and JS can have access to it here's my code

const validFirst = function (inputFirst) {
  let msg;
  let valid = false;
  // au moins 2 caractère
  if (inputFirst.value.length < 2) {
    msg = "Le prénom est trop court";
  } else {
    msg = "le prenom est correct !!!";
    valid = true;
  }
  //Affichage
  //Récupération de la balise Small
  let small = inputFirst.nextElementSibling;

  //on test l'expression régulière
  if (valid) {
    small.innerHtml = "OK 👨🏼‍💻";
    small.classList.remove("text-danger");
    small.classList.add("text-success");
  } else {
    small.innerHtml = "c pas bon cthistoire 🥸";
    small.classList.remove("text-success");
    small.classList.add("text-danger");
  }
};

and the HTML

     <div
        class="formData">
        <label for= "first">Prénom</label>
        <input
          class="text-control"
          type="text"
          id="first"
          name="first"
          minlength="2"
        />
        <small></small>


         </div>

Thanks in advance :)

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

A few console messages show your code is fine up to the point where the text is sent to the page. This allows the error to be found - innerHtml should be innerHTML;

In the working snippet, I've called the function from the input's onchange event (so click outside the input after entering something). If you call by some other method, be sure to check the function receives the element and that each stage is performing the tasks expected of it. I also used 'test' as inner content of the small element to ensure all was as expected.

const validFirst = function (inputFirst) {
  console.log(inputFirst.value.length);
  let msg;
  let valid = false;
  // au moins 2 caractère
  if (inputFirst.value.length < 2) {
    msg = "Le prénom est trop court";
  } else {
    msg = "le prenom est correct !!!";
    valid = true;
  }
  console.log(valid, msg);
  //Affichage
  //Récupération de la balise Small
  let small = inputFirst.nextElementSibling;
  console.log(small.innerText);
  //on test l'expression régulière
  if (valid) {
    small.innerHTML = "OK 👨🏼‍💻";
    //small.classList.remove("text-danger");
    //small.classList.add("text-success");
  } else {
    small.innerHTML = "c pas bon cthistoire 🥸";
    small.classList.remove("text-success");
    small.classList.add("text-danger");
  }
};
     <div
        class="formData">
        <label for= "first">Prénom</label>
        <input
          class="text-control"
          type="text"
          id="first"
          name="first"
          minlength="2"
          onchange="validFirst(this)" 
        />
        <small>test</small>


         </div>

about 4 years ago · Juan Pablo Isaza Report

0

Replace .innerHtml by .innerHTML

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!