Hide submit button if selected parameter value is null

I have the following form:

var x = document.getElementById("submit-button");

if (x.selectedIndex.value == null) {
$("#submit-button").css("display","none");
}
      

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<select>
  <option disabled selected>Select colour</option>
  <option value="Orange">Orange</option>
  <option value="Apple">Apple</option>
  <option value="Lemon">Lemon</option>
</select>
  <button id="submit-button" type="submit">Click</button>
</form>
      

Run codeHide result


If the selected dropdown index is a disabled placeholder with no value, I want to hide the submit button. Once a color is selected, I want to display the button again.

Any help would be appreciated.

+3


source to share


5 answers


You are in the right direction. But some points with events are missing:

1 - All code must be executed when DOM is ready (document loaded)

2 - You must watch the selection event to check the changes



3 - You can use jQuery .hide()

and .show()

control the visibility of elements

// Executed when DOM is loaded
$(document).ready(function() {
   // Executed when select is changed
    $("select").on('change',function() {
        var x = this.selectedIndex;

        if (x == "") {
           $("#submit-button").hide();
        } else {
           $("#submit-button").show();
        }
    });

    // It must not be visible at first time
    $("#submit-button").css("display","none");
}); 

      

Look at the working script

+4


source


The shortest way is



$(function(){
  $("select").on("change", function(){
    $("#submit-button").toggle(!!this.value);
  }).change();
});
      

<script src="//code.jquery.com/jquery-2.1.1.min.js"></script>
<form>
<select>
  <option selected value="">Select colour</option>
  <option value="Orange">Orange</option>
  <option value="Apple">Apple</option>
  <option value="Lemon">Lemon</option>
</select>
  <button id="submit-button" type="submit">Click</button>
</form>
      

Run codeHide result


+1


source


You need to create an event handler for when the dropdown changes. In the example below I have hidden the submit button by default and when the dropdown changes change the visibility of the button based on if the dropdown has a selected value.

I have allowed access to the Select Color option to better demonstrate how the event handler works.

$("#submit-button").hide();

$("select").on("change", function() {
  if (this.value)
    $("#submit-button").show();
  else
    $("#submit-button").hide();
});
    
      

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<select>
  <option selected value="">Select colour</option>
  <option value="Orange">Orange</option>
  <option value="Apple">Apple</option>
  <option value="Lemon">Lemon</option>
</select>
  <button id="submit-button" type="submit">Click</button>
</form>
      

Run codeHide result


0


source


You have to add a listener to your selection like this:

$('form select').on('change', function(){
    if($(this).val() == null){
        $("#submit-button").hide();
    }else{
        $("#submit-button").show();
    }
}).change();

      

It will check every time it changes the parameter, hiding the button when the parameter is irrelevant and showing it when it does. Also, this will cause it to hide first for the initial case.

0


source


Hey. Please try the following solution and tell me if you want something like this:

$(document).ready(function(){
  if($('select').val() == null){
  $('#submit-button').css('display','none');
}

$('select').on('change', function() {
$('#submit-button').css('display','block');
});

      

0


source







All Articles