0

I have a simple javascript code like this, the purpose is to get any elements with the tag name "a" inside element with id "dashboard", and give different argument for the same function with there name.

window.load = function() {
    var a = element("dashboard").getElementsByTagName("a");
    for(x=0;x<10;x++) {
        a[x].addEventListener('click', function() {
            handlerdash(a[x].name);
        });
    }
}

this is inside handlerdash(a[x].name);

function handlerdash(e) {
    console.log(e);
}

and here the html code. the code has been updated..

<div id="dashboard" class="dashboard cell">
    <div class="title">
        <span class="desc">Pengelolaan</span>
    </div>
    <div class="menu">
        <div class="pitem">
            <div class="name">Pengguna</div>
            <div class="citem">
                <a name="gologout">Keluar</a>
                <a name="openusersetting">Pengaturan</a>
            </div>
        </div>
        <div class="pitem">
            <div class="name">Persediaan</div>
            <div class="citem">
                <a name="openaddpsd">Tambah Persediaan Baru</a>
                <a name="openaddstokpsd">Penambahan Stok Persediaan</a>
                <a name="opendatapsd">Data Persediaan</a>
                <a name="opendatamtpsd">Data Mutasi Persediaan</a>
                <a name="openexportpsd">Export Data</a>
            </div>
        </div>
        <div class="pitem">
            <div class="name">Pendistribusian</div>
            <div class="citem">
                <a name="opendispsd">Distribusikan Persediaan</a>
                <a name="opendatadis">Data Pendistribusian</a>
            </div>
        </div>
        <div class="pitem">
            <div class="name">Aplikasi</div>
            <div class="citem">
                <a name="opensetip">Pengaturan IP</a>
                <a name="openbackup">Backup</a>
            </div>
        </div>
    </div>
</div>

the thing that make me confusing, the console is always show

openbackup

even if i click the button with a name that is not 'openbackup' which the procced by the handlerdash(); and openbackup is the last name of elements inside the element with id "dashboard" with tag name "a".

if you asking what inside element();

function element(e) {
    return document.getElementById(e);
}

any help will be apreciated.. thanks for any of your attention..

update

i tried the question that duplicated with mine, and it work correctly. but when i tried to implement to mine it return into error.

c[x] is not a function

i just didnt know how to put the addEventListener();correctly, i changed the code like this.

var a = element("dashboard").getElementsByTagName("a");
var c = [];
for(x=0;x<a.length;x++) {
    a[x].addEventListener('click', function() {
        c[x] = function() { handlerdash(a[x].name); };
    });
}
for(x=0;x<a.length;x++) {
    c[x]();
}

please help.. if i made any mistake..

0 Answers0