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 845965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:32:18+00:00 2026-05-15T06:32:18+00:00

before I ask my question I wanted to let everybody know that I appreciate

  • 0

before I ask my question I wanted to let everybody know that I appreciate the fact that there’s always somebody out there willing to help, and on my end I’ll try to give back to the community as much as I can. Thanks

Now, I would like to get some pointers as to how to properly take advantage of the “for…in” loop in JavaScript, I already did some research and tried a couple things but it is still not clear to me how to properly use it.

Let’s say I have a random number of “select” tags in an HTML form, and I don’t require the user to select an option for all of them, they can leave some untouched if they want.
However I need to know if they selected none or at least one.

The way I’m trying to find out if the user selected any of them is by using the “for…in” loop. For example:

var allSelected = $("select option:selected");
var totalSelected = $("select option:selected").length;

The first variable produces an array of all the selected options.
The second variable tells me how many selected options I have in the form (select tags could be more than one and it changes every time).
Now, in order to see if any has been selected I loop through each element (selected option), and retrieve the “value” attribute. The default “option” tag has a value=”0″, so if any selected option returns a value greater than 0, I know at least one option has been selected, however it does not have to be in order, this is my loop so far:

for(var i = 0; i < totalSelected; i++){
  var eachOption = $(allSelected[i]).val();
  var defaultValue = 0;
  if(eachOption == defaultValue){
    ...redirect to another page
  }else if(eachOption > defaultValue){
    ... I display an alert
  }
}

My problem here is that as soon as the “if” matches a 0 value, it sends the user to the next page without testing the rest of the elements in the array, and the user could have selected the second or third options.

What I really want to do is check all the elements in the array and then take the next action, in my mind this is how I could do it, but I’m not getting it right:

var randomValue = 25;  
for(randomValue in allSelected){
  var found = true;
  var notFound = false
  if(found){
    display an alert
  }else{
    redirect to next page
  }
}

This loop or the logic I’m using are flawed (I’m pretty sure), what I want to do is test all the elements in the array against a single variable and take the next action accordingly.

I hope this makes some sense to you guys, any help would be appreciated.
Thanks,
JC

  • 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-15T06:32:19+00:00Added an answer on May 15, 2026 at 6:32 am

    A simpler way to write (what I think) your trying to achieve using .each()

    // this is to know if we want to redirect
    var redir = true;
    $("select option:selected").each(function() {
      var val = $(this).val();
    
      if (val > 0) {
        alert('Found one!');
      }      
      if (val != 0) { 
        redir = false;
        // you can return false here if you want to stop processing the each loop too!
      } 
    });
    
    if (redir) {
      window.location = "/nextpage";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Before I ask my question, let me just say that I know very little
Before I ask the question let me state that I have attempted to google
Before I ask this question I would to apologise because of the fact that
i already search here before i ask this question.. I know there is an
I wanted to know the basic question that a person needs to ask if
Before I ask the question let me just clarify that the objective of this
Before I ask my question, I want to mention that I am only today
I don't know if this question has been ask before. But I have a
I ask this question because it appears that viewDidLoad gets called before the main
Before ill ask my question I sould mention that i read every post about

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.