JavaScript Function Doesn't Work When Link Is Clicked

Clicking on link with onclick doesn't work

Function "next()" already exists in javascript so it can't be called. Renamed to nextone() works just fine.

call javascript function on hyperlink click

Neater still, instead of the typical href="#" or href="javascript:void" or href="whatever", I think this makes much more sense:

var el = document.getElementById('foo');
el.onclick = showFoo;


function showFoo() {
alert('I am foo!');
return false;
}

<a href="no-javascript.html" title="Get some foo!" id="foo">Show me some foo</a>

If Javascript fails, there is some feedback. Furthermore, erratic behavior (page jumping in the case of href="#", visiting the same page in the case of href="") is eliminated.

Function in JavaScript is not called when loading new page by clicking href url

The problem was caused by jQuery Mobile. Here is the detail: http://jquerymobile.com/demos/1.0a4.1/docs/pages/docs-pages.html

Javascript onclick function is called immediately (not when clicked)?

You want .onclick = secondFunction

NOT .onclick = secondFunction()


The latter calls (executes) secondFunction whereas the former passes a reference to the secondFunction to be called upon the onclick event


function start() {  var a = document.createElement("a");  a.setAttribute("href", "#");  a.onclick = secondFunction;  a.appendChild(document.createTextNode("click me"));  document.body.appendChild(a);}
function secondFunction() { window.alert("hello!");}
start();

How to use a link to call JavaScript?

<a onclick="jsfunction()" href="#">

or

<a onclick="jsfunction()" href="javascript:void(0);">

Edit:

The above response is really not a good solution, having learned a lot about JS since I initially posted. See EndangeredMassa's answer below for the better approach to solving this problem.

a onclick function not working

You can't have both an onClick function and valid href attribute in <a>.
Change your anchor element to:

<a onclick="signOff()" href="javascript:void(0)" data-icon="settings" id="contacts">Log Out</a>

You can redirect the page using javascript if you want to.



Related Topics



Leave a reply



Submit