0

I added a navigaton bar by javascript. It made by html a href, and i want to do something on the onclick event of the href. But when it complied, it runs automaticly insted of wait for the click event.

function topnav() {        
    for (var i=1; i<=menuelemekdb; ++i) {
        if (menuelemek[i]['HOL_VAN']==1) {
            var li = document.createElement("li");
            var aTag=document.createElement("a");
            aTag.setAttribute('href',"#");
            aTag.id=menuelemek[i]['NEV'];
this is the line -->aTag.onclick=opensidenavigaton(menuelemek[i]['ID']);
            aTag.innerHTML=menuelemek[i]['NEV'];
            li.appendChild(aTag);
            topnavid.appendChild(li);
        }
    }
}

Somefunction to call

function opensidenavigaton(HOL_VAN) {
    alert(HOL_VAN);return false;
}

The topnav() function is called onload.

<body onload="topnav()">
    <ul class="topnav" id="topnavigaton" >
        <li class="icon">
            <a href="javascript:void(0);" onclick="menuablakmeret()">&#9776;</a>
        </li>
    </ul>
    <iframe id="iframewindow" class="iframeclass" name="IframeWindow" src="JavaScript:''" style=""></iframe>
</body>
Ivan
  • 2,463
  • 1
  • 20
  • 28
Daniel
  • 45
  • 2
  • 9
  • The linked question talks about `setTimeout` rather than `onclick`, but it's the same problem and the solutions there apply to this. – T.J. Crowder Jan 12 '17 at 13:30
  • @T.J.Crowder deleted the comment, if I changed it to `a.addEventListener("click",function(){somefunction();});` would that have the same issue? – Loaf Jan 12 '17 at 13:50
  • @Loaf: That version's good. If the function doesn't need arguments (and doesn't use any), `a.addEventListener("click", someFunction);` is sufficient. But if it needs args (like the OP's), you wrap it like that (probably passing in the element, either as an arg or as `this`). – T.J. Crowder Jan 12 '17 at 15:31
  • @T.J.Crowder Never knew that about params, thanks! – Loaf Jan 12 '17 at 16:08
  • The solution was that: `aTag.setAttribute("onclick",'somefunction("'+variable+'")');` – Daniel Jan 13 '17 at 13:25

0 Answers0