user1049944
user1049944

Reputation:

jQuery: Hiding *only* text from <label>

I have the following markup that is output from a WordPress plugin

<div class="emaillist">
    <form action="#" method="post">
        <div class="es-field-wrap">
            <label>Name*<br>
                <input type="text" name="name" placeholder="Name" value="" required="" class="form-control">
            </label>
        </div>
        <div class="es-field-wrap">
            <label>Email*<br>
            <input class="form-control" type="email" name="email" value="" placeholder="Email" required=""></label>
        </div>                              

        <input type="submit" name="submit" value="Subscribe">
    </form>
</div>

I am trying to hide the Name and Email text within the <label> fields like so:

$('.form-blog-notify label').text('')

But when I do that, it removes the input fields from the DOM also

Is there a way I can hide the text whilst leaving the text fields in place?

Upvotes: 1

Views: 44

Answers (2)

Twisty
Twisty

Reputation: 30893

You can set the Font Size to 0. Now the text does not show, but still be in the HTML.

.es-field-wrap label {
  font-size: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="emaillist">
  <form action="#" method="post">
    <div class="es-field-wrap">
      <label>Name*<br><input type="text" name="name" placeholder="Name" value="" required="" class="form-control"></label>
    </div>
    <div class="es-field-wrap">
      <label>Email*<br><input class="form-control" type="email" name="email" value="" placeholder="Email" required=""></label>
    </div>
    <input type="submit" name="submit" value="Subscribe">
  </form>
</div>

If you want to remove the text, then jQuery is better.

$(function() {
  $(".es-field-wrap label").each(function(i, el) {
    $(el).contents()[0].remove();
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="emaillist">
  <form action="#" method="post">
    <div class="es-field-wrap">
      <label>Name*<br><input type="text" name="name" placeholder="Name" value="" required="" class="form-control"></label>
    </div>
    <div class="es-field-wrap">
      <label>Email*<br><input class="form-control" type="email" name="email" value="" placeholder="Email" required=""></label>
    </div>
    <input type="submit" name="submit" value="Subscribe">
  </form>
</div>

Upvotes: 0

Pranav C Balan
Pranav C Balan

Reputation: 115222

You need to get the first text node and then remove it. Where you can use contents() method to get all child nodes including text nodes.

$('label').each(function() {
  $(this).contents().first().remove()
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="emaillist">
  <form action="#" method="post">
    <div class="es-field-wrap">
      <label>Name*<br>
                <input type="text" name="name" placeholder="Name" value="" required="" class="form-control">
            </label>
    </div>
    <div class="es-field-wrap">
      <label>Email*<br>
            <input class="form-control" type="email" name="email" value="" placeholder="Email" required=""></label>
    </div>

    <input type="submit" name="submit" value="Subscribe">
  </form>
</div>

Upvotes: 1

Related Questions