Advertisement
bongzilla

Untitled

Jan 28th, 2022
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. <script>
  2. window.addEventListener("DOMContentLoaded", function(e) {
  3. let typeSelect = document.querySelector("select[name='tip']");
  4. let areaSelect = document.querySelector("select[name='pa_rajon']");
  5. let roomSelect = document.querySelector("select[name='kolichestvo-komnat']");
  6.  
  7. function makeEmptyOption(value, text) {
  8. let el = document.createElement("option");
  9. el.value = value;
  10. el.text = text;
  11.  
  12. return el
  13. }
  14.  
  15. typeSelect.add(makeEmptyOption("", "-"), typeSelect.options[0]);
  16. roomSelect.add(makeEmptyOption("", "-"), roomSelect.options[0]);
  17. areaSelect.add(makeEmptyOption("", "-"), areaSelect.options[0]);
  18.  
  19. const searchParams = new URLSearchParams(window.location.search);
  20.  
  21. let typeParam = searchParams.get("tip");
  22. let roomParam = searchParams.get("kolichestvo-komnat");
  23. let areaParam = searchParams.get("pa_rajon");
  24. console.log("typeParam", typeParam);
  25. console.log("roomParam", roomParam);
  26. console.log("roomParam", roomParam);
  27.  
  28. function checkIfEmptyParam(param) {
  29. return param === ""
  30. }
  31.  
  32. function setSelectedOption(el, idx) {
  33. el.options.selectedIndex = idx;
  34. }
  35.  
  36. console.log("checkIfEmptyParam(typeParam)", checkIfEmptyParam(typeParam));
  37. console.log("checkIfEmptyParam(roomParam)", checkIfEmptyParam(roomParam));
  38.  
  39. if (location.pathname === "/shop/") {
  40. setSelectedOption(typeSelect, 0);
  41. setSelectedOption(roomSelect, 0);
  42. setSelectedOption(areaSelect, 0);
  43. } else {
  44. checkIfEmptyParam(typeParam) ? setSelectedOption(typeSelect, 0) : null;
  45. checkIfEmptyParam(roomParam) ? setSelectedOption(roomSelect, 0) : null;
  46. checkIfEmptyParam(areaParam) ? setSelectedOption(areaSelect, 0) : null;
  47. }
  48. });
  49. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement