1

I'm learning Javascript and having trouble. My HTML:

<a id="1" class="same" href="1">click</a>
<a id="1" class="same" href="1">click</a>
<a id="2" class="same" href="1">click</a>
<a id="3" class="same" href="1">click</a>
<a id="3" class="same" href="1">click</a>
<a id="4" class="same" href="1">click</a>

And JS:

var dataall = [];
var a = document.querySelectorAll('.same');
for (var i = 0; i < a.length; i++) {
    var id = a[i].id;
    dataall.push(id);
}
data = dataall.filter(function(elem, pos) {
    return dataall.indexOf(elem) == pos;
})
i = 0;
Click(data, i);

function Click(data, i) {
    setTimeout(function() {
        document.getElementById(data[i]).click();
        if (i < data.length) {
            i++;
            Click(data, i);
        } else {
            alert('done');
        }
    }, 10000)
}

How to click each one for an id 1 2 3 4, not 1 1 2 3 3 4? Thank

5
  • 1
    ID needs to be unique. Commented Aug 27, 2013 at 8:22
  • 2
    IDs are supposed to be unique, why do you have multiple a tags with the same ID in the first place? Commented Aug 27, 2013 at 8:22
  • 2
    And ids can't start with a number. Commented Aug 27, 2013 at 8:22
  • If you're learning JavaScript with this as an example, can I kindly suggest you skip back a chapter or two and consolidate what you already know? Commented Aug 27, 2013 at 8:26
  • My code is very long, so I took the above example I had 6 clicks and clicks may be 4 for 4 id? Commented Aug 27, 2013 at 8:36

1 Answer 1

1

ID need to be unique and can't start with numbers. Still if you want to go with the code.

Try this:

    var dataall = [];
var a = document.querySelectorAll('.same');
for (var i = 0; i < a.length; i++) {
    var id = a[i].id;
    if(dataall.indexOf(id)==-1)
    dataall.push(id);
}
data = dataall.filter(function(elem, pos) {
    return dataall.indexOf(elem) == pos;
})
i = 0;
Click(data, i);

function Click(data, i) {
    setTimeout(function() {
        if (i < data.length) {
             alert("id: "+data[i]);
            document.getElementById(data[i]).click();
            i++;
            Click(data, i);
        } else {
            alert('done');
        }
    }, 100)
}

if(dataall.indexOf(id)==-1) will check if already id is there in array or not. (duplicate values will not be added).

DEMO FIDDLE

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

3 Comments

it gives alert is accurate how to get click through id?
sorry for the delay, I will create a full script your code works fine, please wait for me for a while thank you
i'm sorry, working fine, dataall.indexOf(id)==-1 === data = dataall.filter(function(elem, pos) { return dataall.indexOf(elem) == pos; }), thank you very much

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.