Reputation: 4264
I have the below which is in this fiddle. http://jsfiddle.net/7YFHX/
<ul></ul>
<script>
function test() {
var myNum = [1,2,3,4,5],
myNumTxt = ['one','two','three','four','five'];
return [myNum, myNumTxt];
}
var myTest = test(),
myTestNum = myTest[0],
myTestNumTxt = myTest[1];
console.log(myTestNum);
console.log(myTestNumTxt);
$.each(myTest, function (index, value) {
$("ul").append('<li title="' + value[1] + '">' + value[0] + '</li>');
});
</script>
What I want to return is a list item for each item in my array with the myNumTxt displayed in the title text as shown below.
<ul>
<li title="one">1</li>
<li title="two">2</li>
<li title="three">3</li>
<li title="four">4</li>
<li title="five">5</li>
</ul>
currently I am returning the below.
<ul>
<li title="2">1</li>
<li title="two">one</li>
</ul>
What am I doing wrong here? How can I return both values for each?
Upvotes: 0
Views: 41
Reputation: 21
You can do it like this way,
var listItems={};
var myNum = [1,2,3,4,5];
var myNumTxt = ['one','two','three','four','five'];
$.each(myNum, function(index,value){
createHash(myNumTxt[index],myNum[index])
});
function createHash(key,value){
listItems[key]=value;
}
$.each(listItems, function (key, value) {
$("ul").append('<li title="' + key + '">' + value + '</li>');
});
Upvotes: 0
Reputation: 5056
You are passing myTest to function each
. That is the problem. Try this:
$.each(myTestNum, function (index, value) {
$("ul").append('<li title="' + myTestNumTxt[index] + '">' + value + '</li>');
});
Upvotes: 0
Reputation: 7288
How about using for
loop like below:
var myTest = test();
for(var i = 0; i < myTest[0].length; i++){
$("ul").append('<li title="' + myTest[1][i] + '">' + myTest[0][i] + '</li>');
}
Upvotes: 2