0

Here is my jsp snippet it uses spring mvc

 <form:select path="noOfBankDetails" id = "noOfBankDetails">
 <form:option value="0">0</form:option>
 <form:option value="1">1</form:option>
 <form:option value="2">2</form:option>
 <form:option value="3">3</form:option>
 <form:option value="4">4</form:option>
 </form:select>

 <!-- All the following divs are hidden -->
 <div id = "firstBankDetail"> 
<div id = "secondBankDetail">
<div id = "thirdBankDetail">
<div id = "fourthBankDetail">

Here is my jQuery snippet which shows all the divs when dropdown noOfBankDetails is changed

  $("#noOfBankDetails").change(function(){
           $("#firstBankDetail").show();
           $("#secondBankDetail").show();
           $("#thirdBankDetail").show();
           $("#fourthBankDetail").show();
        });

It works perfectly fine... But i want to achieve something different Pseudo of it goes like this

IF noOfBankDetails IS 0 THEN 
    Show none of divs
ELSE IF noOfBankDetails IS 1 THEN 
    Show div firstBankDetail
ELSE IF noOfBankDetails IS 2 THEN 
    Show div firstBankDetail
    Show div secondBankDetail
and so on..

How should i go about doing this in jQuery. I am a beginner in jQuery. Please provide some guidance.

1
  • You have laid out the logic. Just convert those into jquery code line by line. Thats it.. Worry about optimization later Commented Sep 30, 2013 at 13:49

3 Answers 3

4

jQuery if else is JavaScript if else FYI, anyways:

 $("#noOfBankDetails").change(function(){
  if (this.value == 0) {
      //do 0 stuff
  } else if (this.value == 1) {
      //do 1 stuff
  } else if (value == 2) {
      //do 2 stuff
  }
 });

Or a switch:

$("#noOfBankDetails").change(function(){
    switch(this.value) {
        case 0: //do 0 stuff
        break

        case 1: //do 1 stuff
        break;

        case 2: //do 2 stuff
        break;
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

+1 for the switch suggestion, and saving me finishing typing up an example :)
case-switch (in Chrome) is much slower than if-else -- possibly because it's being converted into if-else statements inside the JS engine.
0

You dont need jQuery for a if/elseif/else. You just can use simple Javascript:

I hope i have no errors. Should work.

$("#noOfBankDetails").change(function(){
  var value = $(this).val();

  if(value == 0) {
    $("#firstBankDetail").show();
  }
  else if(value == 1) {
    $("#secondBankDetail").show();
  }
  else if(value == 2) {
    $("#thirdBankDetail").show();
    $("#fourthBankDetail").show();
  }
});

Comments

0
$("#noOfBankDetails").change(function () {
    if ($(this).val() == 0) {
        $("#firstBankDetail").hide();
        $("#secondBankDetail").hide();
        $("#thirdBankDetail").hide();
        $("#fourthBankDetail").hide();
        $("#secondBankDetail").hide();
        $("#thirdBankDetail").hide();
        $("#fourthBankDetail").hide();
    } else if ($(this).val() == 1) {
        $("#firstBankDetail").show();
    } else if ($(this).val() == 2) {
        $("#firstBankDetail").show();
        $("#secondBankDetail").show();
    } else if ($(this).val() == 3) {
        $("#firstBankDetail")..show();
        $("#secondBankDetail").show();
        $("#secondBankDetail").show();
    }
    //Continue the same logic below
    else if ($(this).val() == 4) {}
});

1 Comment

I think you need to check your code. You're repeating the same actions in some of it.

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.