fishbaitfood Posted November 24, 2011 Share Posted November 24, 2011 Hello there, I have two arrays with an equal amount of values. array fileList: names of files array file_numList: numbers of files I want those values to match their respective other, with a loop. Output would look something like this: for (var i = 0; i < fileList.length ; i++) { $("div.box").append("<input type=\"radio\" value=\""+file_numList[i]+"\" />"+fileList[i]); } This won't work however. And with an .each() loop, I can't seem to get an i counter? How would I do this? Quote Link to comment https://forums.phpfreaks.com/topic/251740-jquery-joining-two-arrays-with-jquery-loop/ Share on other sites More sharing options...
Adam Posted November 25, 2011 Share Posted November 25, 2011 If the keys match, within the callback function you can define two parameters. The first being the key and the second being the value. As an example, run this: <script> $(document).ready(function() { $(['one', 'two', 'three']).each(function(key, value) { alert('Key: ' + key + ' / Value: ' + value); }); }); </script> Using the key you can just look-up the value in the other array. Quote Link to comment https://forums.phpfreaks.com/topic/251740-jquery-joining-two-arrays-with-jquery-loop/#findComment-1291086 Share on other sites More sharing options...
fishbaitfood Posted November 25, 2011 Author Share Posted November 25, 2011 Thanks Adam, I can't believe I couldn't think of that myself! So obvious. Thanks for hinting that out. Quote Link to comment https://forums.phpfreaks.com/topic/251740-jquery-joining-two-arrays-with-jquery-loop/#findComment-1291208 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.