0

There is probably a very simple solution to this but my jQuery skills are not yet up to much. I am using the below to show and hide a div based on the valus on a dropdown. I would like to adapt it so both "No" and "" will work. How do i achive this?

Thanks in advance

$('#q1').change(function(){
 if ($(this).val() == "No") {
 $('#divv').show();
 } else {
 $('#divv').hide();
 }
 });
Legs
  • 41
  • 1
  • 11

6 Answers6

0

You can use the JavaScript logical or operator (||) in this case:

$('#q1').change(function() {
    value = this.value;
    if (value == "No" || value == "") {
        $('#divv').show();
    } else {
        $('#divv').hide();
    }
}
);

Actually the most adaptable solution would be this: if (value.toLowerCase() == "no" || value == "") { This would accept "No" no matter what case it's in.

Demo: http://jsfiddle.net/SO_AMK/YKeFy/1/

EDIT: Updated code to satisfy Kolink and Bergi

Community
  • 1
  • 1
A.M.K
  • 16,727
  • 4
  • 32
  • 64
  • Inefficient due to creating the same `$(this)` object twice. – Niet the Dark Absol Aug 03 '12 at 18:56
  • http://w3fools.com/ - especially for logical operators, there are much better references – Bergi Aug 03 '12 at 18:57
  • @Kolink Please read: http://stackoverflow.com/privileges/vote-down very carefully, you virtually, and maybe literally, downvoted every answer but your own. – A.M.K Aug 03 '12 at 19:19
  • @A.M.K I had reasons to downvote them. This one because of the object inefficiency, but I see that's now been fixed. Now my downvote is there because of the `value == "null"`, which I have no idea why it's there... – Niet the Dark Absol Aug 03 '12 at 19:22
  • I mistakenly thought that if a dropdown item had no value then it would return null, I tested and removed it. – A.M.K Aug 03 '12 at 19:59
0

Use the javascript OR operator (||):

$('#q1').change(function(){
 var thisVal = $(this).val();
 if ((thisVal == "No") || (thisVal == "")) {
 $('#divv').show();
 } else {
 $('#divv').hide();
 }
 });
Curtis
  • 101,612
  • 66
  • 270
  • 352
0

There are a number of ways. Personally I'd do this:

if( this.value.match(/(No)?/i)) {

The i allows NO, no and nO to also work - remove it if you only want "No".

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
0

The logical OR operator should do:

$('#q1').change(function(){
    if (this.value == "No" || this.value == "") {
        $('#divv').show();
    } else {
        $('#divv').hide();
    }
});

I have not used $(this) (especially not twice) to create jQuery objects, but just used plain javascript. Even more concise, if you want (using a tertiary operator, the not operator to check for an empty string and bracket notation):

$('#q1').change(function(){
    $('#divv')[ (this.value == "No" || !this.value) ? "show" : "hide" ]();
});
Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

You can use toggle() and set the state to show or hide by checking the value with a regex.

$('#q1').on('change', function(){
    $('#divv').toggle(this.value.match(/(no|^$)/i));
});

FIDDLE

adeneo
  • 312,895
  • 29
  • 395
  • 388
-1

copy and Paste this, this should work.

$('#q1').change(function(){
 if (($(this).val() == "No") || ($(this).val() == "")) {
 $('#divv').show();
 } else {
 $('#divv').hide();
 }
 });
defau1t
  • 10,593
  • 2
  • 35
  • 47