Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8537343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:52:48+00:00 2026-06-11T10:52:48+00:00

I have a simple jQuery if/else if condition but only the if part works,

  • 0

I have a simple jQuery if/else if condition but only the “if” part works, the “else if” do not, I can’t figure out why. The code represents three listboxes (ajax-populated) and when any of them clicked, a jQuery function collects the ID of the clicked item and assigns it to a variable. The first one (even when I switch places I get the correct ID number) is fine, never the second or the third. It is not working when I use three “if’s” instead of “if/else if” structure either.

$(document).ready(function() {
    $('#btn_edit_details').click(function() {
        var request_id;
        if ($('#lstbx_new_repeat_requests').val().length > 0) {
            request_id = $('#lstbx_new_repeat_requests').val();
            alert(request_id);
        }
        else if ($('#lstbx_approved_repeat_requests').val().length > 0) {
            request_id = $('#lstbx_approved_repeat_requests').val();
            alert(request_id);
        }
        else if ($('#lstbx_declined_repeat_requests').val().length > 0) {
            request_id = $('#lstbx_declined_repeat_requests').val();
            alert(request_id);
        }
if ($('#btn_edit_details').val() == 'edit') {
// rest of the code, working fine when the request_id is passed from the first "if"

And the HTML

<input type='button' id='btn_edit_details' value='edit' />
<select id='lstbx_new_repeat_requests' name='lstbx_new_repeat_requests'></select>
<select id='lstbx_approved_repeat_requests' name='lstbx_approved_repeat_requests'></select>
<select id='lstbx_declined_repeat_requests' name='lstbx_declined_repeat_requests'></select>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:52:49+00:00Added an answer on June 11, 2026 at 10:52 am

    I ended up with the only way it worked for me, though it may look not so smart to you, but again – it’s the only way i could make it work… and i have no idea why it didn’t work with the really basic if/else if way. Any suggestions or comments will be warmly accepted. Thanks all for your efforts to assist.

    The class of the three listboxes is lstbx_repeat_requests. When a user clicks on any of the three, I capture its id in a global var. Then I use a switch statement, as below:

    1. Capture the id:

      $(document).ready(function() {
      $('.lstbx_repeat_requests').change(function() {
          //assigns a global var
          window.my_config =
          {
              lstbx_id : this.id
          };
      });
      });
      
    2. Switch statement:

      $(document).ready(function() {
      $('#btn_edit_details').click(function() {
      
          var request_id;
          switch(window.my_config.lstbx_id){
              case "lstbx_new_repeat_requests":{
                  request_id = $('#lstbx_new_repeat_requests').val();
                  alert(request_id + " new");
                  break;
              }
              case "lstbx_approved_repeat_requests":{
                  request_id = $('#lstbx_approved_repeat_requests').val();
                  alert(request_id + " approved");
                  break;
              }
              case "lstbx_declined_repeat_requests":{
                  request_id = $('#lstbx_declined_repeat_requests').val();
                  alert(request_id + " declined");
                  break;
              }
          }
          //rest of the code
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following is the simple jQuery function but I couldn't figure it out. <html> <head>
I have a simple jQuery script that I'm trying to build upon but I
I have pretty simple jquery code : $(document).ready(function(){ $('img.marqFl').on({ mouseenter: function() { $(this).animate({height: 300},
I have a simple jQuery code that runs on a web page that has
I have a personal simple jQuery validation script. It works when the submit button
I am facing an issue with this simple jQuery code. I have attached the
all. I have built a simple jQuery/PHP chat program that works rather well. However,
I have what i thought was a simple javascript / jquery function (fade out
I have some simple JQuery / Javascript to perform some simple logic for all
I have a simple jquery flipcard animation - http://jsfiddle.net/gGAW5/36/ Now the way this flip

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.