code_junkie

Javascript function to 'get element by parent class' and assign

Nov 11th, 2011
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. function myFunction(element){
  2. liArray = document.getElementById("leftlist").childNodes;
  3. i=0;
  4. while(liArray[i]){
  5. liArray[i].id="";
  6. i++;
  7. }
  8. element.id="current"; // or element.className ?
  9. }
  10.  
  11. /**** css *********/
  12. ul#leftlist {background-color: rgb(205,205,205);}
  13. ul#leftlist li#current a{color: rgb(96,176,255); background-color:218,218,218);}
  14. ul#leftlist li a{ color: rgb(86,86,86);}
  15. #leftlist a:link{color: rgb(86,86,86);background-color: #ddd;}
  16. #leftlist a:active{color: rgb(96,176,255); background-color: rgb(218,218,218); }
  17.  
  18. /****** html *********/
  19. <ul id="leftlist">
  20. <li onClick='myFunction(this);'><a href="123" bla bla </a></li>
  21. <li onClick='myFunction(this);'> .... etc.</li>
  22. </ul>
  23.  
  24. ul#leftlist li.current a {
  25. color: rgb(96,176,255) !important;
  26. background-color:218,218,218) !important;
  27. }
  28.  
  29. ul#leftlist li#current a{color: rgb(96,176,255); background-color:218,218,218);}
  30.  
  31. ul#leftlist li#current a{color: rgb(96,176,255); background-color: rgb(218,218,218);}
  32.  
  33. liArray=element.parentNode.getElementsByTagName("li");
  34.  
  35. function myFunction(element){
  36. var liArray = document.getElementById("leftlist").childNodes;
  37. var i=0, item;
  38. while (item = liArray[i++]) {
  39. if (item.nodeType === 1) {
  40. item.className = item.className.replace(/(^| )current( |$)/g, '');
  41. }
  42. }
  43. element.className += " current";
  44. }
  45.  
  46. ul#leftlist li.current a { ... }
Add Comment
Please, Sign In to add comment