Show submit button on valid data using jQuery

Viewed 79

I need to show the .nexto button once I have valid text in the input. Each and every field has its own button so the thing is when I get a valid value of input the button associated with the field should show and other input fields buttons should hide. Thank you.

var required = $('input[required]');
required.on('keyup', function(event) {
  if (this.value !== "") {
    $(this).$('.nexto').show();
  } else {
    $(this).$('.nexto').hide();
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="typeform-element" id="fname">
  <p><span class="list">1</span>&#8594;What's your First Name? *</p>
  <div class="input-element">
    <input type="text" name="fname" placeholder="Type your answer..." required class="ip-element">
    <p class="blinker">|</p>
  </div>
  <div class="ok-btn">
    <a href="#lname" class="nexto">OK &#10004;</a>
  </div>
</div>
<div class="typeform-element" id="lname">
  <p><span class="list">2</span>&#8594;What's Your Last Name? *</p>
  <div class="input-element">
    <input type="text" name="lname" placeholder="Type your answer..." required class="ip-element">
    <p class="blinker">|</p>
  </div>
  <div class="ok-btn">
    <a href="#email" class="nexto">OK &#10004;</a>
  </div>
</div>

2 Answers

The way you find .nexto element is wrong. From this object inside the event handler, do find closest .typeform-element and then find .nexto child.

Also you need to hide all of the .nexto at start.

$('.nexto').hide()

var required = $('input[required]');
required.on('keyup', function (event) {
    var nexto = $(this).closest('.typeform-element').find('.nexto')
    if (this.value !== "") {
        nexto.show();
    } else {
        nexto.hide();
    }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="typeform-element" id="fname">
    <p><span class="list">1</span>&#8594;What's your First Name? *</p>
    <div class="input-element">
        <input type="text" name="fname" placeholder="Type your answer..." required class="ip-element">
        <p class="blinker">|</p>
    </div>
    <div class="ok-btn">
        <a href="#lname" class="nexto">OK &#10004;</a>
    </div>
</div>

<div class="typeform-element" id="lname">
    <p><span class="list">2</span>&#8594;What's Your Last Name? *</p>
    <div class="input-element">
        <input type="text" name="lname" placeholder="Type your answer..." required class="ip-element">
        <p class="blinker">|</p>
    </div>
    <div class="ok-btn">
        <a href="#email" class="nexto">OK &#10004;</a>
    </div>
</div>

To make this work you need to use jQuery's DOM traversal methods to find the .nexto related to the input which raised the event.

As such you can use a combination of closest(), next() and find(). Also note that you should use CSS to hide those elements by default. In addition, I'd suggest using the input event instead of keyup, as it also works for paste events triggered by the mouse. Try this:

var $required = $('input[required]');

$required.on('input', function(event) {
  $(this).closest('.input-element').next('.ok-btn').find('.nexto').toggle(this.value !== "");
})
.ok-btn .nexto { display: none; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="typeform-element" id="fname">
  <p><span class="list">1</span>&#8594;What's your First Name? *</p>
  <div class="input-element">
    <input type="text" name="fname" placeholder="Type your answer..." required class="ip-element">
    <p class="blinker">|</p>
  </div>
  <div class="ok-btn">
    <a href="#lname" class="nexto">OK &#10004;</a>
  </div>
</div>
<div class="typeform-element" id="lname">
  <p><span class="list">2</span>&#8594;What's Your Last Name? *</p>
  <div class="input-element">
    <input type="text" name="lname" placeholder="Type your answer..." required class="ip-element">
    <p class="blinker">|</p>
  </div>
  <div class="ok-btn">
    <a href="#email" class="nexto">OK &#10004;</a>
  </div>
</div>

Related