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,357
  • 7
  • 39
  • 61

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
  • 320,036
  • 81
  • 464
  • 592
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
  • 21,269
  • 7
  • 56
  • 60
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
  • 897
  • 9
  • 32
0

Here is the working fiddle for you

var s = document.getElementById("myctrl");
s.onclick = function displayIcon() {
Vinod Louis
  • 4,812
  • 1
  • 25
  • 46
0

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

sachin
  • 13,605
  • 14
  • 42
  • 55
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");
}