Because you are navigating to a different page (or reloading the same page) via document.location.href
, you will lose all javascript variable data.
You need to store the information either in a cookie (check out the super simple js-cookie) or as session data.
References:
https://msdn.microsoft.com/en-us/library/ms972338.aspx
In your question, clickId immediately displays as undefined because it was. Assign anything to clickId, and the initial display will have a value.
In your function, clickId's value changes, but you are not displaying it. Once you navigate away from (and then return to) the page, the value is lost, as mentioned above. You must store the clickId in a cookie (or session) for later retrieval.
$(function(){
var clickId=''; //variable is now defined
next();
alert(clickId); //do something with clickId
});
function next(){
$("form").on("click", "div", function(e){
e.preventDefault();
clickId = $(this).attr("id");
//document.location.href='somePage.aspx?id=' + clickId;
alert(clickID);
});
}