user918967
user918967

Reputation: 2167

How to get substring using Dojo and javascript

Good Day,

I am a newbie learning Javascript & Dojo and I typically learn by picking apart other parts of running code.

I am confused as to how to get a substring value from the following code (from the ArcGIS Sandbox):

    <!DOCTYPE html>
      <html>
        <head>
          <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
          <meta http-equiv="X-UA-Compatible" content="IE=7, IE=9, IE=10">
          <!--The viewport meta tag is used to improve the presentation and behavior of the samples 
            on iOS devices-->
          <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
          <title>Query State Info without Map</title>

          <script src="http://js.arcgis.com/3.6/"></script>
          <script>
            dojo.require("esri.tasks.query");
            dojo.require("esri.map");

            var queryTask, query;
            require([
              "esri/tasks/query", "esri/tasks/QueryTask",
              "dojo/dom", "dojo/on", "dojo/domReady!"
            ], function(
              Query, QueryTask,
              dom, on
            ){
              queryTask = new QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5");

              query = new Query();
              query.returnGeometry = false;
              query.outFields = ["SQMI","STATE_NAME","STATE_FIPS","SUB_REGION","STATE_ABBR","POP2000","POP2007","POP00_SQMI","POP07_SQMI","HOUSEHOLDS","MALES","FEMALES","WHITE","BLACK","AMERI_ES","ASIAN","OTHER","HISPANIC","AGE_UNDER5","AGE_5_17","AGE_18_21","AGE_22_29","AGE_30_39","AGE_40_49","AGE_50_64","AGE_65_UP"];

              on(dom.byId("execute"), "click", execute);

              function execute(stateName) {
                query.text = dom.byId("stateName").value;
                //execute query
                queryTask.execute(query, showResults);
              }

              function showResults(results) {
                var s = "";
                for (var i=0, il=results.features.length; i<il; i++) {
                  var featureAttributes = results.features[i].attributes;
                  for (att in featureAttributes) {
                    s = s + "<b>" + att + ":</b>  " + featureAttributes[att] + "<br>";
                  }
                  s = s + "<br>";
                }
                dom.byId("info").innerHTML = s;
              }
            });
          </script>
        </head>
        <body>
          US state name : 
          <input type="text" id="stateName" value="California">
          <input id="execute" type="button" value="Get Details">
          <br />
          <br />
          <div id="info" style="padding:5px; margin:5px; background-color:#eee;">
          </div>
        </body>
      </html>

All I would like to do is pick apart the input (in this case the id="stateName" which is the word California).

So a silly example would be substituting the following code to get the first 10 characters of when someone types in 'California is on the west coast'

     query.text = dom.byId("stateName").substring(0,10);

This is really so I can support other queries but I figured if I can do a substring on this input then it is really the same anytime when I query other attributes.

Thanks in advance for a newbie !

Upvotes: 0

Views: 2511

Answers (3)

Bucket
Bucket

Reputation: 7521

You need to get the innerHTML of your DOM element

query.text = dom.byId("stateName").value.substring(0, 10);

Upvotes: 1

Wyatt
Wyatt

Reputation: 2519

As noted in comments, a call to .value will deliver what you need. Substring is a method on the string prototype See here. However, dom.byId returns a domNode. You don't want the substring of the domNode itself, you want the substring of the text value of the domNode. On inputs this is easily done with .value and is commonly done with .textContent and .innerHTML as well.

Upvotes: 0

user918967
user918967

Reputation: 2167

As Thomas Upton correctly pointed out the correct form would be:

      dom.byId("stateName").value.substring(0, 10);

apparently the following also works

     dom.byId("stateName").value.substr(0, 10);

Upvotes: 0

Related Questions