Select to view content in your preferred language

If then condition with combo boxes in JavaScript API?

2084
18
Jump to solution
10-07-2014 03:24 PM
GeoffreyWest
Occasional Contributor III

I am receiving a syntax error; unexpected identifier at line 5.  @

dijit.byId("cbRequestType").on('change', function (MFValue) {

    if (MFValue == 'Electronic Waste') {

    dijit.byID("cbMFSubType").set('value', 'Copy Machine');

    }

    else if MFValue == '...'{

      dijit.by.ID('cbMFSubType').set('value', 'Printer');

    }

  });

0 Kudos
18 Replies
RobertScheitlin__GISP
MVP Emeritus

Geoffery,

  Are you saying that you did not receive any errors before when you were using dijit.byID?...

Here is a working sample:

<!DOCTYPE html>

<html >

<head>

    <link rel="stylesheet" href="http://js.arcgis.com/3.11/dijit/themes/claro/claro.css">

    <script src="http://js.arcgis.com/3.11/"></script>

   

    <script>

        require([

          "dojo/dom",

          "dojo/on",

          "dojo/parser",

          "dijit/form/ComboBox"

        ],

        function(

          dom, on, parser){

          parser.parse();

          (dijit.byId("cbRequestType")).on('change',function(MFValue){

            if (MFValue == 'E-WASTE') {

              dijit.byId("cbEWASTE").set('value', 'Copy Machine');

            } else if (MFValue == '...') {

              dijit.byId('cbEWASTE').set('value', 'Printer');

            }

          });

        });

    </script>

</head>

<body class="claro">

    <select data-dojo-type="dijit/form/ComboBox" id="cbRequestType" name="RequestType">

      <option>...</option>

      <option>E-WASTE</option>

      <option>Blah</option>

    </select>

    <select data-dojo-type="dijit/form/ComboBox" id="cbEWASTE" name="EWASTE"/>

</body>

</html>

GeoffreyWest
Occasional Contributor III

Hi Robert,

This solution works great, I will try it in my application and give you an update, my initial errors I suppose came from the fact that my syntax was incorrect.  I really appreciate your assistance.    

0 Kudos
GeoffreyWest
Occasional Contributor III

Robert,

Your solution is great, however I think that there was a bit of confusion.

Please see the screen shot.  I am currently passing a layer with several request types.

I would like to populate the description combo boxes with related request types, so I am not necessarily creating new combo boxes as your example indicates, only populating my description cb boxes with hard coded parameters...i.e. copier, etc.

The code that I am working with now populates each of the combo boxes based on the feature layer domain.  Since these descriptions are not coming from my feature layer, I gather that this portion of the code is correct, but I am still missing something to pass to the description combo boxes.

function( 

          dom, on, parser){ 

          parser.parse(); 

          (dijit.byId("cbRequestType")).on('change',function(MFValue){ 

            if (MFValue == 'E-WASTE') { 

              dijit.byId("Description1").set('value', 'Copy Machine'); 

            } else if (MFValue == '...') { 

              dijit.byId('Description1').set('value', 'Printer'); 

            } 

          }); 

        }); 

geonetscreenshare.PNG

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Geoffery,

  I am unsure how to help you more... If the ID of the combobox that you want to change is "Description1" then the code dijit.byId("Description1").set('value', 'Copy Machine'); should work fine (if all you are wanting to do is set the selected value of the combobox). I have asked a couple of time now if this is all you are wanting to do or not, but maybe you are really wanting to add multiple values to the combobox that can be chosen from. Is this the case? If so then look at this sample:

<!DOCTYPE html>

<html >

<head>

    <link rel="stylesheet" href="http://js.arcgis.com/3.11/dijit/themes/claro/claro.css">

    <script src="http://js.arcgis.com/3.11/"></script>

   

    <script>

        require([

          "dojo/dom",

          "dojo/on",

          "dojo/parser",

          "dojo/store/Memory",

          "dijit/form/ComboBox"

        ],

        function(

          dom, on, parser, Memory){

          parser.parse();

         

          var eWasteStore = new Memory({

            data: [

                {name:"copier", id:"1"},

                {name:"scanner", id:"2"},

                {name:"PC", id:"3"},

                {name:"etc", id:"4"}

            ]

          });

          var whiteGoodsStore = new Memory({

            data: [

                {name:"refrigerators", id:"1"},

                {name:"metals", id:"2"},

                {name:"etc", id:"3"}

            ]

          });

         

          (dijit.byId("cbRequestType")).on('change',function(MFValue){

            if (MFValue == 'E-WASTE') {

              dijit.byId("cbEWASTE").setAttribute("store", eWasteStore);

            } else if (MFValue == 'WHITE GOODS') {

              dijit.byId('cbEWASTE').setAttribute("store", whiteGoodsStore);

            }

          });

        });

    </script>

</head>

<body class="claro">

    <select data-dojo-type="dijit/form/ComboBox" id="cbRequestType" name="RequestType">

      <option>...</option>

      <option>E-WASTE</option>

      <option>WHITE GOODS</option>

    </select>

    <select data-dojo-type="dijit/form/ComboBox" id="cbEWASTE" name="EWASTE"/>

</body>

</html>

0 Kudos
GeoffreyWest
Occasional Contributor III

Hi Robert,

I am eternally grateful for your assistance, this seems like the correct solution, I am just having trouble applying to my existing code, thank you so much. 

My only question is at line 47 claro is already defined in my HTML, so I am having a bit of trouble seeing where I'd drop that block, especially if my commodities are already defined and referenced from my feature layer.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Geoffery,

   If you already have "claro" defined for the body of your app then you don't need to do anything.

0 Kudos
GeoffreyWest
Occasional Contributor III

I see,

The issue is that two new combo boxes are created.  Not sure what causes this, I am referencing two cb boxes that are present in the application.  I think the statement in order to retrieve my request type should be data specific and iterate through the layer to grab those fields....I really appreciate your assistance Robert but this is proving to be a bit more difficult than I thought initially.

EDIT: I would like to parse through the applications current feature layer and return the new items in existent cb boxes.

The feature layer maintains the request type and there are description cb boxes that are there to link the two...

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Geoffery,

   The code I provided was just a sample for you to learn from and was not intended to be copied into your existing code. If you need something you can just copy and paste then you will have to provide your full code for me to work with.

0 Kudos
GeoffreyWest
Occasional Contributor III

Robert,

Please follow  me on GeoNet and I will inbox you...I connected with ESRI support regarding the matter and I think that they can offer further assistance from here.  I greatly appreciate your help.    

0 Kudos