0

I created a js program to check a condition using two IF conditions simultaneously. Why doesnt it work? fiddle

    function displayIcon() {
    var a = 1;
    var i = 2;
    if (a == 1) {

        $('.clist').show();
        $('.vPrint').show();
        $('.vFilter').show();
        $('.vSaveAs').show();
        $('.vShare').show();
    } else if (a === 0) {

        $('.vPrint').show();
        $('.vFilter').show();
    }

    if (i == 2) {
        $('.alertInfo').show();
    } else if (i == 4) {
        $('.alertInfo').hide();
    }
}
U.P
  • 7,274
  • 6
  • 36
  • 60

6 Answers6

3

It works just fine for me. All you have to do to make it work in jsFiddle is:

  • Enable the jQuery plugin, so that $ is defined; and
  • Change the function wrapping to "no wrap", otherwise the displayIcon function is only defined within the wrapper and cannot be accessed by the button.
Niet the Dark Absol
  • 311,322
  • 76
  • 447
  • 566
1

Your fiddle had 2 issues:

  • You're calling jQuery functions without loading jQuery
  • Your displayIcon function wasn't defined in the same scope that you were trying to call it from

Here's an updated version that should work.

André Dion
  • 20,347
  • 7
  • 53
  • 59
1

You need to enable jQuery.

You could also consider using a switch:

switch(a) {
    case 1: 
        $('.clist').show();
        $('.vPrint').show();
        $('.vFilter').show();
        $('.vSaveAs').show();
        $('.vShare').show();
        break;
    case 0:
        $('.vPrint').show();
        $('.vFilter').show();
        break;
}
Tro
  • 887
  • 9
  • 32
0

Here is the working fiddle for you

var s = document.getElementById("myctrl");
s.onclick = function displayIcon() {
Vinod Louis
  • 4,742
  • 1
  • 21
  • 44
0

In fiddle change 'onLoad' option into No wrap in body. Then add jquery plugin.

sachin
  • 12,165
  • 13
  • 41
  • 54
0

Thanks everyone, I've got the answer to the question I asked. Two IF conditions can work together. The following code provides the needful.

int a=2, b=3;
if(a<b)
{
alert("B is bigger");
}
if(b<a)
{
alert("A is bigger");
}