0

I go through a list of classes using the filter function. if the drop down is empty then an error styling is added.

problem. I would like to hide and show other div id's if the condition is fulfilled, i.e if all the dropdowns contain a selected value

This is my code for adding styling to an empty dropdown

 $(".dropdown_select").filter(function(){
 return $(this).val() == "";
 }).css("border-color", "#FF0000");

But how do i go about hiding and showing a other two div id's?? i.e only if all dropdowns are filled, i would like to

$(".step-2").fadeIn("slow");
$(".step-1").fadeOut("slow");

2 Answers 2

1

For styling I'd recommend using toggleClass() with a function argument instead of filter() and css():

$(document).on("change", ".dropdown_select", function () {
  var $emptyOnes = $(".dropdown_select")
                   .filter(function () { return this.value == ""; });

  if ($emptyOnes.length == 0) {
    $(".step-1").fadeOut("slow");
    $(".step-2").fadeIn("slow");
  } 

  $(this).toggleClass("empty", this.value == "");
});

where tre CSS definition reads:

.empty {
  border-color: #FF0000;
}
Sign up to request clarification or add additional context in comments.

3 Comments

$emptyOnes.length is always evaluating to 0.....im not sure but as i read i think its because its not real array, from this post stackoverflow.com/a/2528893/1214535 ...have a look at this fiddle jsfiddle.net/theunfeelingsoul/CJAhz/3
No. $emptyOnes.length is only 0 when all .dropdown_select elements have a value. Which is what you wanted (quote "i.e only if all dropdowns are filled, i would like to…").
yea that's what i want but that's not happening, whether the dropdown has a value or not it still evaluates to 0...i dont know why. have a look at the jsfiddle link and see...
1
var $children = $(".dropdown_select").children();

if($children.length === $children.filter(".dropdown_select").length) {
   $(".step-2").fadeIn("slow");
   $(".step-1").fadeOut("slow");
}

1 Comment

I dont understand what $children.filter().length does because is resulting to 0 all the time....??

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.