Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Oct 2010
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    JS selector engine - array issue

    Hello All,

    I am trying to build a small selector engine in order to detect the number of specific html tags, but I am having trouble in storing the number of the found tags in an array and afterwards printing them in the html..

    Can someone please help me out? You can find the code below.

    Code:
      var seltags = function (select) {
      var nodes = [];
      
      var div = document.getElementsByTagName('div');
      var divlen = div.length;
      document.write(divlen+nodes[0]+' DIVs. ');
      
      var img = document.getElementsByTagName('img');
      var imglen = img.length;
      document.write(imglen+nodes[1]+' IMGs. ');
      
      var inp = document.getElementsByTagName('input');
      var inplen = inp.length;
      document.write(inplen+nodes[2]+' INPUTs. ');
      
      for (var i=0, l=var divlen; i<l, i++){
          
      };
    
      //alert(len);
    
      return nodes;
    }
    Now I need some guidance on how to proceed on this because im quite confused and lost.. Im not that experienced in JavaScript so im really struggling.. Any help would be much appreciated.

    Thank you in advance.

  • #2
    Regular Coder
    Join Date
    Mar 2008
    Location
    London
    Posts
    152
    Thanks
    4
    Thanked 42 Times in 42 Posts
    And what is your exact idea behind designing a new "Selector Engine" that too without experienced in JavaScript ??

  • Users who have thanked niralsoni for this post:

    thadeus (02-07-2013)

  • #3
    New to the CF scene
    Join Date
    Oct 2010
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Experience kind Sir.

  • #4
    Regular Coder
    Join Date
    Mar 2008
    Location
    London
    Posts
    152
    Thanks
    4
    Thanked 42 Times in 42 Posts
    Well, I appreciate your affort. But the thing is without having proper knowledge of any programming language, you can't just start implementing your idea.

    I would prefer you to start understanding the basics first, and then come with a clear picture of what your exact problem is.

    Lets investigate what you have provided -

    1) you defined a variable nodes as an array.

    2) then you are trying to access nodes[0], nodes[1], and nodes[2] that too variable nodes without any values stored.

    3) the problem in your for loop syntax (as highlighted in red)-
    for (var i=0, l=var divlen; i<l, i++){
    }

    It should be
    for(var i = 0, l = divlen; i < l; i++) {
    }

    4) No semicolon ( ; ) after end of for loop

    5) The argument select passed in the function is never used, and the function is returning the empty array nodes.

    so, logically, there is no use of this function.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •