How to iterate through children of a node and modify them?
I have an tag of type 'article' in my html page. I want to iterate though all its child and grandchild nodes, and give them an ID if they lack one.
So I tried this code as a test:
- $(document).ready(function () {
- $("#ArticleToHighlightRFS").find("*").each(function (i) {
- alert (this.id);
- });
- })
The idea is that 'find()' would get all descendant nodes that are not text nodes, and would list their ids. If they don't have ids, I suppose the alert box would show 'undefined'.
The above works, but I have to be able to also:
1) note if the element is self-ending, like the linebreak (br) tag. In that case, setting an ID might be a syntax error.
2) set an id if the id is 'undefined'
Any help is appreciated.
Thanks.