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.

 

Level 2
May 21, 2022

It should be added. Can you share your dialog xml?

 

 


Hi,

 

I have added granite:class and I am able to make default value but I have one issue when I open the page properties en/us it's showing default value as en-US when I open de/de page it should be default value as de-DE when i open the page first it is not showing if I load it again then default is setting. 

 

	    $(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";
        }else if(currentUrl.indexOf('de/de') > -1){
			hrefLang = "de-DE";
        }else if(currentUrl.indexOf('jp/ja') > -1){
			hrefLang = "ja-JP";
        }else if(currentUrl.indexOf('int/en/') > -1){
			hrefLang = "en-INT";
        }

        $(".href-lang-dropdown").each(function(index, value) {

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


 

 

  <href_lang
                                                granite:class="href-lang-dropdown"
                                                jcr:primaryType="nt:unstructured"
                                                sling:resourceType="granite/ui/components/coral/foundation/form/select"
                                                fieldLabel="Href language Tags"
                                                name="./hreflangtag"
                                                value="true">
                                                <items jcr:primaryType="nt:unstructured">
                                                    <default
                                                        jcr:primaryType="nt:unstructured"
                                                        text="x-default"
                                                        value="x-default"/>
                                                    <en-US
                                                        jcr:primaryType="nt:unstructured"
                                                        text="en-US"
                                                        value="en-US"/>
                                                    <de-DE
                                                        jcr:primaryType="nt:unstructured"
                                                        text="de-DE"
                                                        value="de-DE"/>
                                                    <fr-FR
                                                        jcr:primaryType="nt:unstructured"
                                                        text="fr-FR"
                                                        value="fr-FR"/>
                                                    <ja-JP
                                                        jcr:primaryType="nt:unstructured"
                                                        text="ja-JP"
                                                        value="ja-JP"/>
                                                    <en-INT
                                                        jcr:primaryType="nt:unstructured"
                                                        text="en-INT"
                                                        value="en-INT"/>
                                                </items>
                                            </href_lang>