Select Default value in coral select field at AEM Page Properties 6.3 | Community
Skip to main content
Level 2
May 19, 2022

Select Default value in coral select field at AEM Page Properties 6.3

  • May 19, 2022
  • 3 replies
  • 3201 views

Hi All,

 

I am trying to set default dropdown value at page properties level where I need to first get the selected value and then change the default value based on current url. Below is the code snippet I tried so far: 

 

 

 

$(document).ready(function() { var currentUrl = window.location.href; var hrefLang = "x-default"; if(currentUrl.indexOf('us/en') > -1){ hrefLang = "en-US"; } $("#coral-id-105").each(function(index, value) { $("#coral-id-105 coral-selectlist-item[value="+ hrefLang +"]").prop('selected', true); }); });

 

 

I am getting undefined every time when I am trying to alert the current value.

Note: I don't need to add value from datasource. I have static values just need to set one as default.

 

@arunpatidar @suraj_kamdi 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

3 replies

Level 2
May 19, 2022

Hi,

 

can anyone help me.

 

Thanks.

Kiran_Vedantam
Community Advisor
Community Advisor
May 19, 2022

Hi @naveen_27_05 

Are you saying that you are not able to get the value selected in the drop-down? Can you be more specific?

Thanks,

Kiran Vedantam.

Level 2
May 22, 2022

Hi Kiran,

 

Yes, I am unable to set the default dropdown value with the js I have shared in the code snippet above.

 

Thank you.

arunpatidar
Community Advisor
Community Advisor
May 19, 2022

Hi,

Your code should look like something below.

This is not tested but just an idea to execute code on pageproperty dialog load event

 

(function (document, $) {
    "use strict";

    
    var currentUrl = decodeURIComponent(window.location.href);
    var hrefLang = "x-default";
    const dropDownSelector = "#coral-id-105"

    $(document).on("foundation-contentloaded", function () {
    setValue(dropDownSelector);
    });

  

    function setValue(dropDownSelector) {
        if(currentUrl.indexOf('us/en') > -1){
            hrefLang = "en-US";          
        }
        $(dropDownSelector).each(function() {
            $(dropDownSelector + " coral-selectlist-item[value="+ hrefLang +"]").prop('selected', true);
        });
    }

})(document, Granite.$);

 

Arun Patidar
Level 2
May 19, 2022

Hi,

 

I have tried with same way for single drop down it's working if we have 3 or 4 dropdown value id getting changed for each value so I have tried with creating the class and I tried executing the same code by changing the id to class but not working.

 

	    $(window).on("load", function() {
      
       // e.preventDefault();
        var currentUrl = window.location.href;
        var hrefLang = "x-default";
        if (currentUrl.indexOf('us/en') > -1) {
            hrefLang = "en-US";
        }else if(currentUrl.indexOf('fr/fr') > -1){
			hrefLang = "fr-FR";
        }
        $(".href-lang-dropdown").each(function(index, value) {

            $(".href-lang-dropdown coral-selectlist-item[value=" + hrefLang + "]").prop('selected', true);
        });
    });

 

Thank you.

 

arunpatidar
Community Advisor
Community Advisor
May 19, 2022

what property did you added?

granite:class for coral3 or class (for coral3)?

you need to check the HTML dom, the custom id/class are added to the corel-select element.

Arun Patidar