Re: [greasemonkey-users] How to change width="...." value for all tags in ONE statement?

Fri, 30 Mar 2012 08:04:46 -0700

Am 30.03.2012 16:35, schrieb Ben Stover:
In the last post you told me how to remove an attribute completely.
Thank you.

1.) But as far as I can from this page:

https://developer.mozilla.org/en/DOM/element.removeAttribute

I have to do this for IDs tags individually.
Is there a way to do this for all DIV tags or TABLE tags in ONE statement?

In other words is there a way to code:

document.getElementById("div").removeAttribute("align");

or

document.getElementById("div*").removeAttribute("align");

or

document.getElementById("table*").removeAttribute("align");


2.) Furthermoe: How does that statement lloks like if I want to change it e.g. 
from

<table width="565px" ....>

to

<table width="98%" ....>

Thank you
Ben

Hm, it's difficult to answer because I'm not sure you understood what an ID is etc. In the code

<table width="90%" id="navigation" name="navtable" class="prettytable nav">...</table>

there are four ways to get elements with a "getElement(s)By":

1) The tag name ("table"): document.getElementsByTagName("table") - this is a list of ALL table-nodes in the document 2) The id ("navigation"): document.getElementById("navigation") - this is exactly ONE element that has this identifier 3) The name ("navtable"): document.getElementsByName("navtable") - this is a list of all elements with that name; opposed to ids, names may be assigned multiple times and also to elements of various tag-names 4) The class name (for example "prettytable"): document.getElementsByClassName("prettyTable") - this Mozilla-specific (!) function returns a list of all elements that have this class name assigned.

You can also apply that function on an specific element instead of the whole document to only search in the element's child nodes.

So, "getElementById" is likely not what you meant, because a question "how do I use getElementById to get all ... elements" makes no sense ;) However I've got a feeling that

document.querySelectorAll("")

might be the function you're wishing for. The "query" is of the same syntax like CSS selectors, thus this is a very mighty function. To remove the width-attribute from all table and div elements, you'd put this:

rmWidthAttribute = function () {
        document.removeEventListener("load", rmWidthAttribute);
        var elements = document.querySelectorAll("table, div");
        var i = 0;
        while (i < elements.length) {
            elements[i].removeAttribute("width");
            i++;
    }
}

document.addEventListener("load", rmWidthAttribute, true);

To only affect tables with width 536px, use

document.querySelectorAll('table[width="536px"]');

and so on, just like the usual CSS selector.

Chris

--
You received this message because you are subscribed to the Google Groups 
"greasemonkey-users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/greasemonkey-users?hl=en.

Reply via email to