user1879057
user1879057

Reputation:

if/else with select value in jquery

I have got a "kategorie" select, and I try to get te value of selected answers. Then by this value my website need to change the css atribute.

I don't know what is wrong, but this isnt working. Please,help me. Ps. Sorry for my english.

    $('select#kategorie').change(function() {
    var value = $('select#kategorie:selected').val();
    if (value=="1" || value=="10")
    {
    $("#liczba-pokoi").css("display","block");
    }
    if else (value=="2" || value=="11")
    {
    $("#liczba-pokoi").css("display","none");
    }
    if else ((value=="3" || value=="12")
    {
    $("#liczba-pokoi").css("display","none");
    }
    if else (value=="4" || value=="13")
    {
    $("#liczba-pokoi").css("display","block");
    }
    });

Upvotes: 1

Views: 4178

Answers (4)

luke
luke

Reputation: 321

A tidier solution:

var val, liczba_pokoi = $("#liczba-pokoi");

$('#kategorie').change(function() {

  val = $(this).val();

  if(val == 1 || val == 4 || val == 10 || val == 13) liczba_pokoi.show();
  if(val == 2 || val == 3 || val == 11 || val == 12) liczba_pokoi.hide();  

});

See fiddle: http://jsfiddle.net/pBxfX/

Upvotes: 2

jogesh_pi
jogesh_pi

Reputation: 9782

Your if else condition should be else if instead of if else

$('select#kategorie').change(function() {

    var value = $('select#kategorie:selected').val();

if (value=="1" || value=="10")
{
$("#liczba-pokoi").css("display","block");
}
else if (value=="2" || value=="11")
{
$("#liczba-pokoi").css("display","none");
}
else if ((value=="3" || value=="12")
{
$("#liczba-pokoi").css("display","none");
}
else if (value=="4" || value=="13")
{
$("#liczba-pokoi").css("display","block");
}
});

follow the link to know more. IF ELSE Statement

Upvotes: 0

xdazz
xdazz

Reputation: 160833

else if, not if else.

You should check the syntax error first.

Upvotes: 2

Hanky Panky
Hanky Panky

Reputation: 46900

You are using If Else with an incorrect syntax. It has to be like

$('select#kategorie').change(function() {
    var value = $(this).val();
    if(value=="1" || value=="10")
    {
       $("#liczba-pokoi").css("display","block");
    }
    else if(value=="2" || value=="11")
    {
       $("#liczba-pokoi").css("display","none");
    }
    else if((value=="3" || value=="12")
    {
       $("#liczba-pokoi").css("display","none");
    }
    else if(value=="4" || value=="13")
    {
       $("#liczba-pokoi").css("display","block");
    }
  });

Upvotes: 1

Related Questions