Guest User

Untitled

a guest
Jan 12th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. var spConfig = new Product.Config(<?php echo $_jsonConfig ?>);
  2.  
  3. //we create new function
  4. spConfig.setInitialState = function (dropdown_id)
  5. {
  6. //select dropdown
  7. var dropdown = $(dropdown_id);
  8. //remove empty option from dropdown so it is not selectable after initial selection
  9. // dropdown[0].remove();
  10. //alert(dropdown.length);
  11. if (dropdown.length == 2) {
  12. dropdown[0].remove();// for the removing choose an option
  13. }
  14. //change selections in dropdowns
  15. for (index = 0; index < dropdown.length; index++)
  16. {
  17. if (dropdown[index].value != "")
  18. {
  19. dropdown.selectedIndex = index;
  20. var element = dropdown;
  21. var event = 'change';
  22. //fire events
  23. if (document.createEventObject)
  24. {
  25. var evt = document.createEventObject();
  26. return element.fireEvent('on' + event, evt)
  27. } else
  28. {
  29. var evt = document.createEvent("HTMLEvents");
  30. evt.initEvent(event, true, true);
  31. return !element.dispatchEvent(evt);
  32. }
  33. }
  34. }
  35. };
  36. <?php foreach ($_attributes as $_attribute): ?>
  37. spConfig.setInitialState("attribute<?php echo $_attribute->getAttributeId() ?>")
  38. <?php endforeach; ?>
  39.  
  40. <script>
  41. /* Andrew Davie 100118
  42. Where there is only one option left in the dropdowns, autoselect it.
  43. */
  44. require(['jquery'], function($){
  45. $('select[id^="attribute"]').change(function(){
  46. var picked = this;
  47. setTimeout(function() {
  48. autoselect(picked); //do the autoselect after everything else.
  49. }, 0);
  50.  
  51. });
  52. function autoselect(picked){
  53. var nextsel = $(picked).parent().parent().next().find('select[id^="attribute"]');
  54. if($(nextsel).children().length==2){
  55. $(nextsel).find(':nth-child(2)', picked).prop('selected',true);
  56. $(nextsel).change();
  57. };
  58. };
  59. });
  60. </script>
  61. <?= $block->getChildHtml() ?>
Add Comment
Please, Sign In to add comment