I am new to javascript and I have been playing with a script I am trying to get work by reading through and following examples on w3c Schools, which is working well as I can get basics to work. However I feel my noob coding needs to be reduced (tidied up) and I am stuck with a list calculation that will only work if all variables are present.
So I am here for your help.
Here is an example basic list, there are 20 items in this example and this will be the max in any one list, however there can be less than 20 list items.
I have some code that sets a unique ID to each list item as provided by another DD forum user;Code:<ul id="mylist"> <li>Text1</li> <li>Text2</li> <li>Text3</li> <li>Text4</li> <li>Text5</li> <li>Text6</li> <li>Text7</li> <li>Text8</li> <li>Text9</li> <li>Text10</li> <li>Text11</li> <li>Text12</li> <li>Text13</li> <li>Text14</li> <li>Text15</li> <li>Text16</li> <li>Text17</li> <li>Text18</li> <li>Text19</li> <li>Text20</li> </ul>
so now each list item has an item_# id assigned to them, # being the incremental number of list items starting from 1Code:<script type="text/javascript"> /*<![CDATA[*/ function postlist(id,cls){ var ul=document.getElementById(id); var lis=ul.getElementsByTagName('LI'); for (var z0=0;z0<lis.length;z0++){ lis[z0].id=cls+(z0+1); } } postlist('mylist','item_'); /*]]>*/ </script>
Then I wanted to calculate the first three sets of five list items overall height (which will be variable as the actual text in each list item will differ) as this is used to enable me to place each set in its own column of 5 list items.
So my noob code :P
The result is an over-ride style item I need for the layout I am working with, which works for the 20 list items, however if the list is less than 20 items, like only 12 items or even 6 items etc then it does not work. I assume that it is because there is less than 20 list items and the above code is trying to run as if there was 20 items and hence has with nothing to run on, as in there is no item_13 etc if say I only have 6 items.Code:<script type="text/javascript"> var postHt1 = document.getElementById("item_1").clientHeight; var postHt2 = document.getElementById("item_2").clientHeight; var postHt3 = document.getElementById("item_3").clientHeight; var postHt4 = document.getElementById("item_4").clientHeight; var postHt5 = document.getElementById("item_5").clientHeight; var postHt6 = document.getElementById("item_6").clientHeight; var postHt7 = document.getElementById("item_7").clientHeight; var postHt8 = document.getElementById("item_8").clientHeight; var postHt9 = document.getElementById("item_9").clientHeight; var postHt10 = document.getElementById("item_10").clientHeight; var postHt11 = document.getElementById("item_11").clientHeight; var postHt12 = document.getElementById("item_12").clientHeight; var postHt13 = document.getElementById("item_13").clientHeight; var postHt14 = document.getElementById("item_14").clientHeight; var postHt15 = document.getElementById("item_15").clientHeight; postttl1 = postHt1 + postHt2 + postHt3 + postHt4 + postHt5 - 1; postttl2 = postHt6 + postHt7 + postHt8 + postHt9 + postHt10; postttl3 = postHt11 + postHt12 + postHt13 + postHt14 + postHt15 - 1; document.write("<style>#item_6{margin-top:-"); document.write(postttl1); document.write("px !important;}</style>"); document.write("<style>#item_11{margin-top:-"); document.write(postttl2); document.write("px !important;}</style>"); document.write("<style>#item_16{margin-top:-"); document.write(postttl3); document.write("px !important;}</style>"); </script>
I hope I make sense and someone can guide and teach me.
Cheers
GW



I like Smilies
Reply With Quote


Bookmarks