Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Iframe change select option texts

The "when page loads" part of the code works fine.

The "When selection is made" part of the code works but seems to stop after a couple of selection changes. Would like it to work on both select boxes as many times as the user changes the selections.

https://jsfiddle.net/czqpkL6j/1/

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 //when page loads 
setTimeout(function() {           
      var iframePreLoad = $("#product-component-1635439141053").children().contents();
      iframePreLoad.find(".shopify-buy__option-select__select").ready(function(){
        iframePreLoad.find('select[name=Color] option:nth-child(1)').html('Black');  
        iframePreLoad.find('select[name=Color] option:nth-child(2)').html('Green');
      }); 

//When selection is made
       iframePreLoad.find('select').on('change', function() {
        setTimeout(function() {
            iframePreLoad.find('select[name=Color] option:nth-child(1)').html('Black');  
          iframePreLoad.find('select[name=Color] option:nth-child(2)').html('Green');
        },100)
         

        console.log('selection changed');
       });
}, 3000);

>Solution :

Not sure what’s the surrounding here exactly but a wild guess based on experience: do the elements potentially change after the iframePreLoad.find(..) selector resolved? If so, nodes matching the selector won’t be in the result set (for obvious reasons) and thus have no change handler attached.

The usual approach to problems like this is to make use of event bubbling and attach the listener to a container surrounding the elements you want to observe. That way you will catch events even from elements changed or added afterwards.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading