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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Apr 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Collect all divs in array based on regex

    Hello,

    I am trying to get all the div tags in my page into an array but only the tags with the ID "image**". Below is what I came up with but this doesn't seem to work, even after googling a bit I couldn't find any solution. I don't think this should be so hard but I'm very new to JavaScript (tbh programming in general) and can't seem to come up with a solution.

    Any help or pointers would be greatly appreciated.

    Code:
    function divs() {
    	
    	var divs = document.getElementsByTagName('div');
    	var ImageTags = [];
    	
    	for (i=0; document.getElementsByTagName('div').length; i++) {
    		
    		var div = divs[i];
    		
    		if (/image[0-9]*/.test(div)) {
    			
    			imageTags.push(div);
    		}
    	}
    }

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,314
    Thanks
    203
    Thanked 2,565 Times in 2,543 Posts
    for (i=0; document.getElementsByTagName('div').length; i++) {
    var div = divs[i];


    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,314
    Thanks
    203
    Thanked 2,565 Times in 2,543 Posts
    for (i=0; document.getElementsByTagName('div').length; i++) {
    var div = divs[i];

    You should avoid giving names or id's to your variables/functions/arguments/forms words which are JavaScript methods/properties/attributes such as 'name' or 'id' or 'value' or 'text' or 'checked' or 'submit' or 'replace' or 'button' or 'radio' or 'parseInt'.



    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.


  •  

    Posting Permissions

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