Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. "use strict";
  2.  
  3. /*
  4. New Perspectives on HTML5, CSS3, and JavaScript 6th Edition
  5. Tutorial 11
  6. Case Problem 1
  7. Author: Megan Rasmussen
  8. Date: 03/31/2020
  9.  
  10. Filename: bw_review.js
  11.  
  12. Functions List:
  13. init()
  14. Initializes the contents of the web page and sets up the
  15. event handlers.
  16.  
  17. lightStars(e)
  18. Function that colors the stars representing the user rating
  19. for the book.
  20.  
  21. turnOffStars(e)
  22. Function that restores the stars to empty colors, removing
  23. the user rating for the book.
  24. updateCount()
  25. Updates the count of characters in the wordCountBox
  26. element.
  27. countCharacters(textStr)
  28. Returns the number of a non-whitespace characters
  29. within textStr
  30. */
  31.  
  32. window.onload = init;
  33.  
  34. function init() {
  35. stars[i].style.cursor = "pointer";
  36. stars[i].addEventListener("mouseenter", lightStars());
  37.  
  38. document.getElementById ("comment").addEventListener ("keyup", updateCount());
  39.  
  40. function lightStars(e) {
  41. var starNumber = e.target.alt;
  42. var stars = document.querySelectorAll("span#stars img");
  43. for(var i = 0; i < starNumber; i++) {
  44. stars[i].setAttribute("src","bw_star2.png");
  45. }
  46. document.getElementById("Rating").value="i"
  47.  
  48. }
  49.  
  50. function turnOffStars(e) {
  51. var stars = document.querySelectorAll("span#stars img");
  52. for(var i = starNumber.length; i < 5; i++) {
  53. stars[i].setAttribute("src", "bw_star.png");
  54. }
  55. document.getElementById("Rating").value="";
  56. }
  57. var wordCount = document.getElementById("wordCount");
  58. wordCount.value = "";
  59. var comment = document.getElementById("comment");
  60. comment.value = "";
  61.  
  62. document.getElementById("Rating").value = starNumber.length + "stars";
  63. e.target.addEventListener("mouseleave", turnOffStars);
  64.  
  65.  
  66. function updateCount() {
  67. var commentText = document.getElementById("comment").value;
  68. var charCount = countCharacters(commentText);
  69. var wordCountBox = document.getElementById("wordCount");
  70. wordCountBox.value = charCount + "/1000";
  71. if(charCount > 1000) {
  72. wordCountBox.style.color = "white";
  73. wordCountBox.style.backgroundColor = "red";
  74. }
  75. else {
  76. wordCountBox.style.color = "black";
  77. wordCountBox.style.backgroundColor = "white";
  78. }
  79. }
  80. }
  81.  
  82. /*=================================================================*/
  83.  
  84. function countCharacters(textStr) {
  85. var commentregx = /\s/g;
  86. var chars = textStr.replace(commentregx, "");
  87. return chars.length;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement