Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Laracast13's avatar

jquery foreach loop with two array

Hello , How can I loop two array in one foreach loop

//List for select
       TomList = ["A","B"],
		 BobList = ["C", "D"],

//
ListA = ["Tom","Bob"],
ListB = ["TomList","BobList"],

///////////////
// //////  I try these two loops, but I think this is wrong
/////////
		$.each(ListA, function(indexA, valueA) {	
			 $.each(ListB, function(indexB, valueB) {

			if(selectedVal == "' + valueA + '") {
				$.each(valueB, function(index, value) {
					_options += '<option value="' + value + '" >' + value + '</option>';
				});

			 });			
			});
			}

it must loop like


			if(selectedVal == "Tom") {
				$.each(TomList, function(index, value) {
					_options += '<option value="' + value + '" >' + value + '</option>';
				});
			if(selectedVal == "Bob") {
				$.each(BobList, function(index, value) {
					_options += '<option value="' + value + '" >' + value + '</option>';
				});
0 likes
5 replies
tykus's avatar

It is really difficult to understand the context here. Do you have dependent dropdowns? Are you attempting to populate one or two select elements?

Laracast13's avatar

I have two select, In select 1 have Tom and Bob, and populating select 2 .

For ex. if select Tom create slecte 2 with options

if(selectedVal == "' + valueA + '") { There we need but Tom and this $.each(valueB, function(index, value) { creates option list for tom using array TomList

Laracast13's avatar

if(selectedVal == "' + valueA + '") { I want check which value is set from array ListA

for set ex. Tom if(selectedVal == "Tom") {

Then $.each(valueB, function(index, value) { create option list using array ListB $.each(TomList, function(index, value) { set TomList which load data from array TomList

Laracast13's avatar

Hi if(selectedVal == "' + valueA + '") { I want check which value is set from array ListA

for set ex. Tom if(selectedVal == "Tom") {

Then $.each(valueB, function(index, value) { create option list using array ListB $.each(TomList, function(index, value) { set TomList which load data from array TomList

Laracast13's avatar

Sorry , I have problem with website and when I post replys it not showing for me.

Please or to participate in this conversation.