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

133
Views
In html input label * add detect to required field using JavaScript/jQuery

enter image description here

I have multiple input form but need to * to add in label, not manually add using javascript

 <label for="form_name">Firstname</label>
    <input id="form_name" type="text" name="name" class="form-control" placeholder="Firstname" required="">
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Add a CSS class first and then add it to your input

$('input[required]').each(function(){
      $($(this).parent()).find('label').addClass('required') //depending on the structure of your fields
});
.required:after{
    color:red;
    content:" * ";
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<label for="form_name">First name</label>
    <input id="form_name" type="text" name="name" class="form-control" placeholder="First name" required="">

about 4 years ago · Juan Pablo Isaza Report

0

Reverse the order of the elements (and correct the positioning using CSS), this allows for using the adjacent sibling combinator +:

.form-group { 
  display: grid;
  grid-template-areas:
    "label"
    "input";
}

:required+label::after { content: " *"; color: teal; }

.form-group label { grid-area: label; }
.form-group .form-control { grid-area: input; }
<div class="form-group">
  <input id="form_name" type="text" name="name" class="form-control" placeholder="Firstname" required="">
  <label for="form_name">Firstname</label>
</div>

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!