0

Through jQuery I would like to find a way to disable an input ONLY IF EMPTY on_ click of a button below.

This is the input:

<input type="text" min="0" id="toolset-maps-distance-center" name="toolset_maps_distance_center" class="form-control js-toolset-maps-distance-center js-wpv-filter-trigger" value="" required="" placeholder="Inserisci una posizione" autocomplete="off">

This is the button:

<button type="submit" class="wpv-submit-trigger js-wpv-submit-trigger btn">Recherche</button>

This is my jQuery:

jQuery('.wpv-submit-trigger.js-wpv-submit-trigger.btn').click(function(){
  jQuery('#toolset-maps-distance-center').prop('disabled', true);
});

The result in the console is:

Uncaught SyntaxError: Invalid or unexpected token

4
  • Is true\false how it is in your code? It should be either true or false. Commented Jun 6, 2018 at 10:59
  • I am sorry i leave the past attempt, now is correct. Commented Jun 6, 2018 at 11:02
  • Or try using attr('diabled', 'disabled') instead of prop Commented Jun 6, 2018 at 11:50
  • Thank you so much code_Ninja, but i tried the PiTiNiNjA code and it works perfectly Commented Jun 6, 2018 at 12:06

4 Answers 4

1
$('.wpv-submit-trigger.js-wpv-submit-trigger.btn').click(function() {
    //check if input is empty
    var ifInputIsEmpty = $('#toolset-maps-distance-center').val() === '';
    //set disabled attribute according to ifInputIsEmpty
    $('#toolset-maps-distance-center').prop('disabled', ifInputIsEmpty);
});
Sign up to request clarification or add additional context in comments.

3 Comments

you should always add an explanation to your answer, this helps avoid a copy/paste generation of developers and helps the OP to understand their mistake
This is perfect, Thank you
@ThisGuyHasTwoThumbs Your comment was faster than my edit ;)
0

please try this. Check the value of input field and if it is empty, just disable it.

jQuery('.wpv-submit-trigger.js-wpv-submit-trigger.btn').click(function(){
  var inputValue=jQuery('#toolset-maps-distance-center').val();
    if(!inputValue)
    {        
      jQuery('#toolset-maps-distance-center').attr('disabled','true');
    }
});

1 Comment

Thank you so much, but i tried the PiTiNiNjA code and it works perfectly
0

try this code

check input value in if condition

if ($('#toolset-maps-distance-center').val() == '')

jQuery('.wpv-submit-trigger.js-wpv-submit-trigger.btn').click(function() {
  if ($('#toolset-maps-distance-center').val() == '') {
    jQuery('#toolset-maps-distance-center').prop('disabled', true);
  }

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" min="0" id="toolset-maps-distance-center" name="toolset_maps_distance_center" class="form-control js-toolset-maps-distance-center js-wpv-filter-trigger" value="" required="" placeholder="Inserisci una posizione" autocomplete="off">
<button type="submit" class="wpv-submit-trigger js-wpv-submit-trigger btn">Recherche</button>

1 Comment

Thank you so much, but i tried the PiTiNiNjA code and it works perfectly
0

As per i understand, you want the submit button to disabled when input is empty. For that try this:

if ($.trim($('.kind').val()).length === 0){
  $('.kind').prop('disabled', true);
}
<input class="kind" id="name" name="name" type="text" placeholder="Kind">

    <button class="submit" >Submit</button>

1 Comment

Thank you so much, but i tried the PiTiNiNjA code and it works perfectly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.