[jQuery] Re: copy li values to an array
Jonathan and Karl : thanks for your replies. Jonathan : I probably gave the impression that I wanted each 3rd li value copied to each array element. Your code worked brilliantly for that and I've stored it away for another day. Karl: that's exactly what I wanted. Problem solved. (BTW I use your 'Reference Guide' all the time) On 5 Feb, 01:00, Karl Swedberg [EMAIL PROTECTED] wrote: Oops. I forgot to take out the ul:first part of the selector. I had put that in there when I was testing on a random page in Firebug, so you can safely take it out. Also, if your LIs just have text in them, you can use $(this).text() instead of $(this).html() --Karl On Feb 4, 2008, at 7:10 PM, Karl Swedberg wrote: Hi Paul, I may have misunderstood what you were going for, but here is a different approach, just in case you were looking for an array like ['abc', 'def', 'ghi']: var txt = '', array1 = []; $('ul:first li').each(function(i){ txt = txt + $(this).html(); if (i % 3 == 2) { array1.push(txt); txt = ''; } }); --Karl _ Karl Swedberg www.englishrules.com www.learningjquery.com On Feb 4, 2008, at 5:00 PM, Jonathan Sharp wrote: Hi Paul, This should do the trick: $('li').each(function(i){ array1[ Math.floor( i / 3 ) ] = $(this).html(); }); Cheers, -Jonathan On 2/4/08, Paul Jones [EMAIL PROTECTED] wrote: I know the following would work if I wanted to copy the values of *each* li to a separate array element. html head title/title script type = text/javascript src=jquery.js/script script type = text/javascript var array1 = new Array() ; $(document).ready(function() { $('li').each(function(i) { array1[i] = this.innerHTML ) }) }) /script /head body ul lia/li lib/li lic/li lid/li lie/li lif/li lig/li lih/li lii/li /ul /body /html However, I would like like to copy the *concatenated* values of each group of 3 li's to 1 array element. (eg) the 1st array element would have a value of 'abc', the 2nd array element would have a value of 'def', and the 3rd array element would have a value of 'ghi' etc. What is the best way to do this? TIA
[jQuery] Re: copy li values to an array
Hi Paul, This should do the trick: $('li').each(function(i){ array1[ Math.floor( i / 3 ) ] = $(this).html(); }); Cheers, -Jonathan On 2/4/08, Paul Jones [EMAIL PROTECTED] wrote: I know the following would work if I wanted to copy the values of *each* li to a separate array element. html head title/title script type = text/javascript src=jquery.js/script script type = text/javascript var array1 = new Array() ; $(document).ready(function() { $('li').each(function(i) { array1[i] = this.innerHTML ) }) }) /script /head body ul lia/li lib/li lic/li lid/li lie/li lif/li lig/li lih/li lii/li /ul /body /html However, I would like like to copy the *concatenated* values of each group of 3 li's to 1 array element. (eg) the 1st array element would have a value of 'abc', the 2nd array element would have a value of 'def', and the 3rd array element would have a value of 'ghi' etc. What is the best way to do this? TIA
[jQuery] Re: copy li values to an array
Hi Paul, I may have misunderstood what you were going for, but here is a different approach, just in case you were looking for an array like ['abc', 'def', 'ghi']: var txt = '', array1 = []; $('ul:first li').each(function(i){ txt = txt + $(this).html(); if (i % 3 == 2) { array1.push(txt); txt = ''; } }); --Karl _ Karl Swedberg www.englishrules.com www.learningjquery.com On Feb 4, 2008, at 5:00 PM, Jonathan Sharp wrote: Hi Paul, This should do the trick: $('li').each(function(i){ array1[ Math.floor( i / 3 ) ] = $(this).html(); }); Cheers, -Jonathan On 2/4/08, Paul Jones [EMAIL PROTECTED] wrote: I know the following would work if I wanted to copy the values of *each* li to a separate array element. html head title/title script type = text/javascript src=jquery.js/script script type = text/javascript var array1 = new Array() ; $(document).ready(function() { $('li').each(function(i) { array1[i] = this.innerHTML ) }) }) /script /head body ul lia/li lib/li lic/li lid/li lie/li lif/li lig/li lih/li lii/li /ul /body /html However, I would like like to copy the *concatenated* values of each group of 3 li's to 1 array element. (eg) the 1st array element would have a value of 'abc', the 2nd array element would have a value of 'def', and the 3rd array element would have a value of 'ghi' etc. What is the best way to do this? TIA
[jQuery] Re: copy li values to an array
Oops. I forgot to take out the ul:first part of the selector. I had put that in there when I was testing on a random page in Firebug, so you can safely take it out. Also, if your LIs just have text in them, you can use $(this).text() instead of $(this).html() --Karl On Feb 4, 2008, at 7:10 PM, Karl Swedberg wrote: Hi Paul, I may have misunderstood what you were going for, but here is a different approach, just in case you were looking for an array like ['abc', 'def', 'ghi']: var txt = '', array1 = []; $('ul:first li').each(function(i){ txt = txt + $(this).html(); if (i % 3 == 2) { array1.push(txt); txt = ''; } }); --Karl _ Karl Swedberg www.englishrules.com www.learningjquery.com On Feb 4, 2008, at 5:00 PM, Jonathan Sharp wrote: Hi Paul, This should do the trick: $('li').each(function(i){ array1[ Math.floor( i / 3 ) ] = $(this).html(); }); Cheers, -Jonathan On 2/4/08, Paul Jones [EMAIL PROTECTED] wrote: I know the following would work if I wanted to copy the values of *each* li to a separate array element. html head title/title script type = text/javascript src=jquery.js/script script type = text/javascript var array1 = new Array() ; $(document).ready(function() { $('li').each(function(i) { array1[i] = this.innerHTML ) }) }) /script /head body ul lia/li lib/li lic/li lid/li lie/li lif/li lig/li lih/li lii/li /ul /body /html However, I would like like to copy the *concatenated* values of each group of 3 li's to 1 array element. (eg) the 1st array element would have a value of 'abc', the 2nd array element would have a value of 'def', and the 3rd array element would have a value of 'ghi' etc. What is the best way to do this? TIA