0

How can show (*) beside the label only for input required field..? required is not a class in my case..?

i have tried like this but not working

input[required] label:after { content:" (*)";color:red; }
<div>
    <label>Name:</label><br>
    <input type="text" name="name" required>
</div>
    

<br>

<div>
    <label>Address:</label><br>
    <input type="text" name="address" >
</div>
Mr world wide
  • 4,696
  • 7
  • 43
  • 97

3 Answers3

3

You can do that with jQuery so that you don't need to change your markup. So after the page loads you can do like this

$('input[required]').prev('label').append('*');

To be able to customize the asterisk you can do this:

$('input[required]').prev('label').append('<font class="requiredAsterisk">*</font>');

/* And customize it in your CSS */
font.requiredAsterisk{
    color: red;
}
Luca De Nardi
  • 2,280
  • 16
  • 35
1

You could do something like this:

.wrapper > div{
  display: flex;
  flex-direction: column-reverse;
}

input:required + label::before{
  content: "* "
}
<div class="wrapper">
<div>
    <input type="text" name="name" required>
    <label>Name:</label><br>
</div>

<div>
    <input type="text" name="address" >
    <label>Address:</label><br>
</div>
</div>

http://jsfiddle.net/zbe1k4ay/

priya_singh
  • 2,478
  • 1
  • 14
  • 32
Kushtrim
  • 1,899
  • 6
  • 14
0

Try this.

jQuery("[required]").prev('label').append("<span>*</span>");
label{
  display:block;
}
label span{
  color:#ff0000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
    <label>Name:</label>
    <input type="text" name="name" required>
</div>
<br/>
<div>
    <label>Address:</label>
    <input type="text" name="address" >
</div>
ankita patel
  • 4,201
  • 1
  • 13
  • 28