SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Addict tlacaelelrl's Avatar
    Join Date
    Apr 2011
    Location
    Mexico city, Mexico
    Posts
    353
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    removeChild based on tagnode.map is not a function error

    I have a function that selects all elements with tag a and removes the parentīs parent node if a class is applied to them however I am getting an error in firebug that states it is not a function

    Code JavaScript:
    function groupSelectedElements() {
        document.getElementById(form_values[0]).getElementsByTagName('a').map(function (value) {
    		if (value.className === 'checkbox checkbox_c') {
    		document.getElementById(form_values[0]).removeChild(value.parentNode.parentNode.id);
    		}
    	});
    }

    firebug says

    Code:
    document.getElementById(form_values[0]).getElementsByTagName("a").map is not a function
    groupSelectedElements()form_create.php (line 440)
    onclick()4 (line 3)
    event = click clientX=748, clientY=182

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,687
    Mentioned
    100 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by tlacaelelrl View Post
    CODE]document.getElementById(form_values[0]).getElementsByTagName("a").map is not a function
    What you have there with getElementsByTagName is node list, not an array.

    One way to deal with that is to explicitly call the array map method and pass the node list to it.

    Code javascript:
    var els = document.getElementById(form_values[0]).getElementsByTagName('a');
    Array.prototype.map(els, function () {
        ...
    });

    If you find that some web browsers don't support the array map method that you are using there (internet explorer being one of them), then the array.map() page has compatibility code for that.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Addict tlacaelelrl's Avatar
    Join Date
    Apr 2011
    Location
    Mexico city, Mexico
    Posts
    353
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    What you have there with getElementsByTagName is node list, not an array.

    One way to deal with that is to explicitly call the array map method and pass the node list to it.

    Code javascript:
    var els = document.getElementById(form_values[0]).getElementsByTagName('a');
    Array.prototype.map(els, function () {
        ...
    });

    If you find that some web browsers don't support the array map method that you are using there (internet explorer being one of them), then the array.map() page has compatibility code for that.
    Thank you so much, that worked, this is what the function ended up like:

    Code JavaScript:
    function groupSelectedElements() {
        Array.prototype.slice.call(document.getElementById(form_values[0]).getElementsByTagName('a')).map(function (value) {
            if (value.className === 'checkbox checkbox_c') {
            document.getElementById(form_values[0]).removeChild(document.getElementById(value.parentNode.parentNode.id));
            }
    	});
    }
    Do you get bothered because I do the same thing every day?
    Do you question why I do it?
    Then find something that you actually like doing!!!

    Stop thinking on what I do.


Bookmarks

Posting Permissions

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