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

  • SEARCH
  • Home
  • 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 4614912
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:45:57+00:00 2026-05-22T01:45:57+00:00

I have a form that has a few select dropdowns, and I want to

  • 0

I have a form that has a few select dropdowns, and I want to make sure that at least one has the default of ‘0’ changed to something else. However, I can’t get my mind around how to do it. I’ve tried both .each() and a while statement, and neither behaves like I expect it to.

Here is the HTML:

<form>
        <label>Qty:</label>
        <select class="Qty">
            <option selected="selected">0</option>
            <option>1</option>

       </select>
       <br />
       <label>Qty:</label>
        <select class="Qty">
            <option selected="selected">0</option>
            <option>1</option>
       </select>
       <br />
       <br />
       <label>Qty:</label>
        <select class="Qty">
            <option selected="selected">0</option>
            <option>1</option>
       </select>
       <br />
       <a href="#">test</a>
</form>

and here is the while statement:

 $(document).ready(function(){
    $('a').click(function(e){
        e.preventDefault();
        while ($('select > option:selected').val()==0){
                alert('hello');

            } 
    });

Unless all the selects are set to something other than 0, it gets stuck in a loop where it keeps testing the selects (so the hello alert keeps coming up).

I tried each as well.

$('a').click(function(e){
        e.preventDefault();
        $('select > option:selected').each(function (i) {
            if($(this).val()>0) {
                alert('okay to proceed');
            } else {
                alert('nothing found');
            }
        });
    });

This is a little better, but it keeps checking even after its found the true answer. So if I try to trigger an error message, it will say everything is okay but then trigger the error message still.
I also tried setting a variable for if its valid or not, but it keeps showing up as false:

$('a').click(function(e){
        e.preventDefault();
        var valid = false;
        $('select > option:selected').each(function (i) {
            if($(this).val()>0) {
                var valid= true;
                alert(valid);
            }
        });
        if(valid==true) {
            alert(valid);
            alert('it works');
        } else {
            alert(valid);
            alert('uh-oh');
        }
    });

I’m clearly missing something. Any assistance on this would be greatly appreciated.

  • 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-05-22T01:45:58+00:00Added an answer on May 22, 2026 at 1:45 am

    In your first try, the while statement, it will keep looping because:

    $('select > option:selected').val() will always ==0 and it never leaves the loop.

    For the .each you can break the loop when it is found by return false as mentioned in the documentation.

    For the last, you declare var inside the .each(function() which means it is only defined within that function and as soon as it goes to the next each in the selection the var will be redefined — once it has exited the .each it is no longer declared.

    So working off your last example: jsfiddle

     $('a').click(function(e){
            e.preventDefault();
         var found = false;
         $('select').each(function(){
             if($(this).val()!=0){
                 found = true;
                 return false;
             }
         });
         if(found)
         {
             alert('success');
         }
         else
         {
             alert('failed');
         }
     });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form that has a few similar controls and the parent contains
I have a web form that has multiple ListBoxes, TextBoxes, DropDowns. If I put
I have a form with a table that has two input boxes, a select
I have a form that has multiple fields, and for testing purposes is there
I have a form that has a public property public bool cancelSearch = false;
I have a form in C# that has a button that, when clicked, I
If I have a form-backing object that has a complicated object tree -- say
I have a form that is dynamically generated, and has dynamically generated id's (and
I have a site that has a simple HTML button in a form. All
I have an Access form where each record has some info that is computed

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.