2

I'm having this issue with javascript right now where if I would console log the element itself it will show the length in chrome but it doesn't show the length correctly when I use .length.

Chrome console

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>Hello World - Google  Web Search API Sample</title>
    <script src="https://www.google.com/jsapi"
        type="text/javascript"></script>
    <script language="Javascript" type="text/javascript">
 
    google.load('search', '1');

    function OnLoad() {
 
      var searchControl = new google.search.SearchControl();
   
      var localSearch = new google.search.LocalSearch();
   
      searchControl.addSearcher(new google.search.WebSearch());
   
      localSearch.setCenterPoint("New York, NY");
   
      searchControl.draw(document.getElementById("searchcontrol"));
   
      searchControl.execute("a");
    }
    google.setOnLoadCallback(OnLoad);
    </script>
  </head>
  <body onLoad="myFunction()">
    <div id="searchcontrol">Loading</div>
    <script>
function myFunction() {
     var y = document.getElementsByClassName('gsc-webResult gsc-result');
     
     var y2 = y.length;
     console.log(y);
     console.log(y2);
}
</script>
  </body>
</html>

As seen above I'm saving the element in var y and saving the length of y in y2. Then I'm console logging these variables, but it doesn't show the right length.

I'm not sure what the problem is here, I hope you can help.

Edit: Added working source code.

Kevib
  • 37
  • 3
  • Your snippet is deceiving, because there are no items it will always log 0. Please add some example content to see if it is actually logging the incorrect length, since I have _never_ seen Chrome do something like incorrect logging of length. – somethinghere Nov 05 '15 at 13:51
  • 1
    We'll need to see a working snippet.. can you create a fiddle? Are those elements populated later on by any chance? – Omri Aharon Nov 05 '15 at 13:53
  • I see `1` here, what wrong? – vp_arth Nov 05 '15 at 13:57
  • @vp_arth I concur, this is doing what it is supposed to do. _It's working_ – somethinghere Nov 05 '15 at 13:58
  • 1
    You're going to have to be much clearer about what you expect to see, vs what you're actually seeing. All you're doing in your question right now is describing **correct behavior** with no explanation for why you think it's incorrect. – user229044 Nov 05 '15 at 13:59
  • May be `body.onLoad` called before any google query is finished. Just wait for it. – vp_arth Nov 05 '15 at 14:02
  • Also my chrome reported about `allow-same-origin` politic(localStorage access). And about deprecation: `The Google Web Search API has been officially deprecated.`. – vp_arth Nov 05 '15 at 14:05
  • It's not duplicate. It's not a bug even. Issue with OP inattention. Array and length correctly logged as `[]` and `0`, because at time you call `console.log` array is empty. Then you click to expand array at the moment it filled. Chrome show us object's state at the time we expand it. And blue icon warn us about this behavior. – vp_arth Nov 06 '15 at 05:21

1 Answers1

-1
var y = document.getElementsByClassName('gsc-webResult gsc-result');

Returns an array of all the elements that match your query. Therefore y.length shows you the length of the array, not the length of the element.

try y[0] assuming that the element you want is first element in the array. Then length on the value assuming you want length of the text.

Also, if you only need a single element you may want to make your query more specific - but it's hard to say since you didn't post the corresponding DOM tree.

j-u-s-t-i-n
  • 1,402
  • 1
  • 10
  • 16
  • 2
    Why are people upvoting this? An element with a `length`? What are you rambling about? Why would you want the length of an `element element`? And how does that _in any way_ solve the problem that `y.length` does not equal `y.length` according to the OP? – somethinghere Nov 05 '15 at 13:55
  • 1
    The question has been modified since my answer... but thanks for your kind and insightful comments :) – j-u-s-t-i-n Nov 05 '15 at 14:03
  • Should also note that my answer was relevant and answered the OP before edits... – j-u-s-t-i-n Nov 05 '15 at 14:15
  • It was irrevelavant, you are lie... https://stackoverflow.com/revisions/33546281/1 He never said any `element length` about. – vp_arth Nov 06 '15 at 05:16
  • @vp_arth Read the link you posted: "if I would console log the element itself it will show the length". Kids today, lol. – j-u-s-t-i-n Nov 06 '15 at 12:34