Guest User

Untitled

a guest
Jun 19th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. ## Given the following html [html]
  2.  
  3. <a href="#" id="older">Older</a> <a href="#" id="newer">Newer</a>
  4. <div id="foo_1" style="display: none">...</div>
  5. <div id="foo_4" style="display: none">...</div>
  6. <div id="foo_8" style="display: visible">...</div>
  7. <div id="foo_11" style="display: none">...</div>
  8. <div id="foo_57" style="display: none">...</div>
  9.  
  10. ## Goal: [javascript]
  11. // I want to hide the currently visible div, and show the previous or the next div,
  12. // depending on the click
  13. // I currently have this working code
  14.  
  15. $('a#older').live('click', function() {
  16. var div = $(this).parent().children('div:visible:first');
  17. var next = $(div).next('div:hidden');
  18. if(next.length != 0) {
  19. next.show();
  20. } else {
  21. $(this).parent().children('div:hidden:first').show();
  22. }
  23. $(div).hide();
  24. return false;
  25. });
  26.  
  27. $('a#newer').live('click', function() {
  28. var div = $(this).parent().children('div:visible:first')
  29. var prev = $(div).prev('div:hidden');
  30. if(prev.length != 0) {
  31. prev.show();
  32. } else {
  33. $(this).parent().children('div:hidden:last').show();
  34. }
  35. $(div).hide();
  36. return false;
  37. });
  38.  
  39.  
  40. ## [plain_text]
  41. I wondered if there is a better way in jquery. Any takers?
Add Comment
Please, Sign In to add comment