Advertisement
weighter

JavaScript Quick Reference Sheet

Dec 15th, 2021 (edited)
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //===============================================================================
  2. // JavaScript snippets
  3.  
  4. // document loaded listener
  5. document.addEventListener("DOMContentLoaded", addDocumentListeners);
  6.  
  7. // Create Selectors
  8. const processBtn = document.querySelector("#processBtn")
  9.  
  10. // Hide an adjacent element via DOM and CSS
  11. nameSelector.nextElementSibling.classList.add("hide");
  12.  
  13. // set radio button checked item
  14.   document.querySelector("#city-none").checked = true;
  15.  
  16. // get value for which item is checked in radio buttons
  17. document.querySelector('input[name="city"]:checked').value;
  18.  
  19. // add click event listener
  20. processBtn.addEventListener("click", validateFunction)
  21.  
  22. // shortcut to target any item inside a div
  23. const toggleFunction = e =>{
  24.   e.currentTarget.parentNode.children[1].classList.toggle("hide");
  25.   e.currentTarget.parentNode.children[2].classList.toggle("hide");
  26.   e.currentTarget.parentNode.children[3].classList.toggle("hide");
  27.   e.currentTarget.parentNode.children[4].classList.toggle("hide");
  28. }
  29.  
  30. //jQuery Quick Reference
  31. //=============================================================================
  32.  
  33. // document ready check
  34. $(document).ready(
  35.     () => {
  36.         functionName();
  37.     }
  38. )
  39.  
  40. // Regex check
  41.     const locationSelector = document.querySelector("#locationID");
  42.     letregexTest = /^[1-9][0-9]{5}$/.test(locationSelector.value);
  43.  
  44.  
  45. // Getting values from a form field in jquery
  46. $("[name='name']").val(name);
  47.  
  48. const radio1 = $("#radio1");
  49. const radio2 = $("#radio2");
  50. const radioOutputField = $("#radioOuput");
  51.  
  52. $("#btnTarget").click(
  53.    evt => {
  54.       if (radio1.is(":checked")){
  55.         radioOutputField.val("Message 1");
  56.       }
  57.       else if (radio2.is(":checked")){
  58.         radioOutputField.val("Message2")
  59.       }
  60. }
  61.  
  62.  
  63.  
  64. // round to 2 decimals
  65. numberVar.toFixed(2)
  66.  
  67. // Radio Button to textbox
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement