How do I get value of a node to use as a variable to set a default value for an address search?

3245
4
08-12-2015 02:53 PM
ChrisSergent
Regular Contributor III

I have the following block of code where I ewant to set the node value based on the focus event so that I can reuse this block of code. It works when I use this line: node = document.getElementById('ownerAddress');

but not when I try to set node to a variable. I also have node declared as a global variable. I thought I could access it since I returned the value in my getElementId function and then use the node.value in the checkAddress function, but there is no value at that time. What am I doing wrong in this block?

 on(document.getElementById('ownerAddress'), 'focusout', checkAddress);


                 on(document.getElementById('ownerAddress'), 'focusin', getElementId);


                 function getElementId() {
                     console.log(document.activeElement.id);
                     
                     node = document.activeElement.id;
                     console.log(node);
                     return node;
                 }




                 function checkAddress() {
                     var locator = new Locator("http://maps.decaturil.gov/arcgis/rest/services/Public/WebAddressLocator/GeocodeServer");
                     //console.log(document.getElementById('ownerAddress').value);
                     //node = document.getElementById('ownerAddress');
                     // according to your service it takes Single Line
                     var params = {
                         "Single Line Input": node.value
                     };
Tags (2)
0 Kudos
4 Replies
ChrisSergent
Regular Contributor III

It looks like I needed to remove my return statement and then wrap document.getElementById around the global variable node. I decided to leave this up in case someone wants to use a value from another function for use in a current function.

I needed to modify my code like follows:

on(document.getElementById('ownerAddress'), 'focusout', checkAddress);


                 on(document.getElementById('ownerAddress'), 'focusin', getElementId);


                 function getElementId() {
                     // I am not sure why I can't access this value when I attempt to get the node.value


                     console.log(document.activeElement.id);
                                          
                     node = document.activeElement.id;
                     
                    // return node;
                 }




                 function checkAddress() {
                     console.log(node);
                     var locator = new Locator("http://maps.decaturil.gov/arcgis/rest/services/Public/WebAddressLocator/GeocodeServer");
                     //console.log(document.getElementById('ownerAddress').value);
                     //node = document.getElementById('ownerAddress');
                     node = document.getElementById(node);
                     // according to your service it takes Single Line
                     var params = {
                         "Single Line Input": node.value
                     };

You were right Kelly Hutchins​.

0 Kudos
RobertWinterbottom
Occasional Contributor

Another thing you may be able to try is to check the event that gets passed to checkAddress, it has lots of useful properties for things like this.  For example:

on(document.getElementById('ownerAddress'), 'focusout', checkAddress);

function checkAddress (event) {
  console.dir(event);
  // event.target or event.srcElement will both point to the node that triggered the event, a.k.a. ownerAddress
  // There are also a lot of other properties attached to event that can also be useful
}

In this case you don't have to do anything special to get event into checkAddress, it is automatically passed in for you (unless you use Angular).

ChrisSergent
Regular Contributor III

I am not sure how I would apply it here, but that is an interesting feature.

0 Kudos
RobertWinterbottom
Occasional Contributor

Ahh ok, maybe I misunderstood the question.  Im not sure if this is what you were looking for but this is a nice way to get a reference to the node so you can get or set its value or just hold on to the node without needing to use document.getElementById.  The event that gets passed in has an event.target property which is actually the dom node, ownerAddress.