I need to assign an ID to a variable from inside a click function but carry that assignment as well from outside the scope of the function, although i get an 'undefined' if i test for it outside of the click function.(Im not asking about the answer below,my fault)
// var for button
var $firstMobButton;
$(".preLoaderTest").click(function(){
var checkDiv = $(this);
if(checkDiv.attr("id") == "showPreLoader"){
$firstMobButton = $("#showPreLoader");
alert($firstMobButton); // object
}else if(checkDiv.attr("id") == "showPreLoaderDebt"){
$firstMobButton = $("#showPreLoaderDebt");
alert($firstMobButton); // object
}
});
// this function doesnt assign the variable to the id OUTSIDE the If statement ...thats what i meant, Sorry i didnt explain well, i didnt mean what it is on instant load, i mean what is it after this function gets carried out)
solved:
$(".preLoaderTest").click(function(){
var checkDiv = $(this);
if(checkDiv.attr("id") == "showPreLoader"){ // check for an id for this partial view
the best way was not to try and assign an ID but to give the button a class then test later if the button had an ID, thanks to Quentin for putting me on the right track...
if you're interested, the full solution:
// grab dataSource for unselected value
var $yourAge = yourAge.view(); // change this dataSource value
// function call on click if onSelect empty html
$(".preLoaderTest").click(function(){
var checkDiv = $(this);
if(checkDiv.attr("id") == "showPreLoader"){ // check for an id for this partial view
if($("#displayNextSelection").html() === "") {
$("#displayNextSelection").html('<p class="selectFeedBack">You age: ' + '<b>' + $yourAge[0].text + '</b>' + '</p>');
$disDeskSel2.html('<p><span class="label label-primary label-ls">Based on your selection </span> You age: ' + $yourAge[0].text + '</p>')
.hide();
}
}
});
You assign a value to
$firstMobButton
when preLoaderTest is clicked.You try to alert it immediately (which is going to be before then, since you've only just registered the event handler).
JavaScript can't violate the rules of linear time!