Question

First of all, sorry for bad english.

Well, I have 3 paragraphs, with some links in each paragraphs, and I have to show the links by paragraph

heres my HTML & JS code:

<head>
    <script type="text/javascript">
            function pEn(){
        var nump=document.getElementsByTagName("p");
        var nEn=new Array();

        for (var i = 0; i < nump.length; i++) {
        nEn=document.getElementsByTagName("a");
        for (var j = 0; j < nEn.length; j++) {
                alert("Parrafo numero "+(i+1)+": "+nEn[j]);
        };
        };
            }
    </script>
</head>

<body>
    <p>
    <a href="http://www.google.es">First link</a>
    <a href="http://www.stackoverflow.com">Second link</a>
    </p>

    <p>
    <a href="http://www.neoteo.com">Third link</a>
    </p>

    <p>
    <a href="http://fp.edu.gva.es/">Fourth link</a>
    </p>

    <div>
    <button onclick="pEn()">Links by paragraph</button>
    </div>
</body>

What i want to get is: P1: www.fdsjkfls.com, P1: www.fjkdslfjsklo.com, P2: www.sdklf.com, P3: www.vnsdwo.com

Not this: P1: www.fdsjkfls.com, P1: www.fjkdslfjsklo.com, P1: www.sdklf.com, P1: www.vnsdwo.com, P2: www.fdsjkfls.com, P2: www.fjkdslfjsklo.com, P2: www.sdklf.com, P2: www.vnsdwo.com, ...

Any sugestions?

Was it helpful?

Solution

Select for links in each paragraph instead :

for (var i = 0; i < nump.length; i++) {
    nEn=nump[i].getElementsByTagName("a"); // <-- Here use nump[i] instead of document
    for (var j = 0; j < nEn.length; j++) {
            alert("Parrafo numero "+(i+1)+": "+nEn[j]);
    };
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top