Toggle hidden text inside multiple elements


/ Published in: JavaScript
Save to your folder(s)

HTML: <a href="#"></a>


Copy this code and paste it in your HTML
  1. function toggleTitle(b) {
  2. //alert(links.length);
  3. var spans = b.getElementsByTagName('span');
  4. function removeChildrenFromNode(node) {
  5. if(node.hasChildNodes()) {
  6. while(node.childNodes.length >= 2) {
  7. node.removeChild(node.lastChild);
  8. }
  9. }
  10. }
  11.  
  12. if (spans.length != 0) {
  13. removeChildrenFromNode(b);
  14. }
  15. else {
  16. if (b.hasAttribute('title')) {var annotationText = '<span class=\"annotation\"><br><i>Annotation: </i>'+b.getAttribute("title")+'</span>';} else {var annotationText = '';};
  17. if (b.hasAttribute('keywords')) {var keywordsText = '<span class=\"authors\"><br><i>Authors: </i>'+b.getAttribute("keywords")+'</span>';} else {var keywordsText = '';};
  18. if (b.hasAttribute('authors')) {var authorsText = '<span class=\"keywords\"><br><i>Keywords: </i>'+b.getAttribute("authors")+'</span>';} else {var authorsText = '';};
  19. b.innerHTML += authorsText+"\n"+annotationText+"\n"+keywordsText;
  20. }
  21. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.