code_junkie

Handling a toggled onclick with JQuery

Nov 13th, 2011
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. <span id='blah' onclick='showAllComment("this is a long comment to see it all", 9, true )'>this is a...</span>
  2.  
  3. <script>
  4. function showAllComment( comment, shortCommentLen, showFullComment )
  5. {
  6. alert( $("#blah").html() );
  7.  
  8. if( showFullComment )
  9. {
  10. $("#blah").html( comment );
  11. $("#blah").click( showAllComment( comment, shortCommentLen, false ) );
  12. }
  13. else
  14. {
  15. $("#blah").html( comment.substring( 0, shortCommentLen ) + "..." );
  16. $("#blah").click( showAllComment( comment, shortCommentLen, true ) );
  17. }
  18. }
  19. </script>
  20.  
  21. function showAllComment( comment, shortCommentLen, showFullComment )
  22. {
  23. alert( $("#blah").html() );
  24.  
  25. if( showFullComment )
  26. {
  27. $("#blah").html( comment );
  28. $("#blah").click( function () { showAllComment(comment, shortCommentLen, false);} );
  29. }
  30. else
  31. {
  32. $("#blah").html( comment.substring( 0, shortCommentLen ) + "..." );
  33. $("#blah").click( function () {showAllComment( comment, shortCommentLen, true );} );
  34. }
  35. }
  36.  
  37. $(function() {
  38. $(".blah").each( function() {
  39. var shortCommentLen = 9;
  40. var comment = $(this).html();
  41. $(this).html(shortComment(comment, shortCommentLen));
  42. $(this).toggle(
  43. function() { $(this).html(comment); },
  44. function() { $(this).html(shortComment(comment, shortCommentLen)); }
  45. );
  46.  
  47. function shortComment(comment, shortCommentLen) {
  48. return comment.substring( 0, shortCommentLen ) + "...";
  49. }
  50. });
  51. });
  52.  
  53. <span class='blah'>this is a long comment to see it all</span>
Add Comment
Please, Sign In to add comment