user3659748
user3659748

Reputation: 9

change a global variable value in a function jquery

I have a global variable and I want to change its value in a jQuery function like this:

var namep ='';

$("#page2").bind('pageshow', function (event , data) {
   var perso = $(this).data("url").split("?")[1];;
   perso_name = perso.replace("perso=","");
   namep=perso_name;
   $("#header h2").text(perso_name); 
});

alert(namep);

In my alert I have nothing so I think namep doesn't change. How can I change its value?

Upvotes: 0

Views: 1581

Answers (1)

PeterKA
PeterKA

Reputation: 24638

It does get changed; it's just that you're checking it too soon -- before it is changed. You should put the alert in the handler, where the value is changed:

var namep ='';

$("#page2").bind('pageshow', function (event , data) {
    var perso = $(this).data("url").split("?")[1];;
    perso_name = perso.replace("perso=","");
    namep=perso_name;
    alert(namep);
    $("#header h2").text(perso_name); 
});

Upvotes: 2

Related Questions