2

In a web page,i want to get every visible text in a textnode.I don't want to put all the result into one array.I mean, when i meet a visible text, i will do something else. How could i achieve it?

1
  • I think you need to be more clear with what you are trying to do. Do you want to iterate through the DOM and perform an action on every textnode you encounter? Do you mean HTML form text input field? Commented Mar 31, 2010 at 15:10

1 Answer 1

2

I guess you want something like this:

<script type="text/javascript">
function textnodes(){
    function iterate(node){
        var nodes=node.childNodes
        var len=nodes.length
        for(var a=0;a<len;a++){
            if(nodes[a].nodeType==3){
                if(!nodes[a].nodeValue.match(/^[\s]*$/)){
                    alert(nodes[a].nodeValue) //Insert your code here.
                }
            }
            else{
                if(nodes[a].nodeName.toLowerCase()!="script"){
                    iterate(nodes[a])
                }
            }
        }
    }
    iterate(document.body)
}
textnodes()
</script>

The script as it is might be a bit overzealous, you get along a lot of invisible text nodes, you can sort those out if you don't need them.

Edit: Modified to sort out invisible nodes since you specifically requested only visible nodes.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.