Ad

Prevent Function Calling Before Hyperlink Clicked

- 1 answer

My problem is that the function runs before I clicked the hyperlink. Here I create the link dynamically:

   $("#nearByCitiesDiv").append('<a data-role="button"  target="_blank" rel="nofollow noreferrer" href="#/" onclick="' + showNearCityWeather(nearbyPosition) + '; return false;" data-theme="a">' + c.toponymName + '</a >'); 

and here is the function:

var showNearCityWeather = function (pos)
        {
        alert("asd");
        }
Ad

Answer

It assumes you want to append the value returned by the function to the onclick. Change the code to this.

onclick="showNearCityWeather('+nearbyPosition+')"

So your complete code must be.

$("body").append('<a data-role="button"  target="_blank" rel="nofollow noreferrer" href="#/" onclick="showNearCityWeather('+nearbyPosition+'); return false;" data-theme="a">' + c.toponymName + '</a >'); 

Note the change where the function name just a string rather than being the actually function call.

Ad
source: stackoverflow.com
Ad