how to save and use variable in jquery [Resolved]

Posted by Klbaiju under jQuery on 10/22/2016 | Points: 10 | Views : 1962 | Status : [Member] | Replies : 2
Hi,

following is a working code for displaying the value in 2 div's disp and bus.

$.ajax({
type: "POST",
// url: "Busbooking.aspx/ConductorTable",
url: "Busdetails.aspx/BindData1",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {

var s = msg.d;
var k = s.split("--");

$('#disp').html(k[0]);
filter1();
$('#bus').html(k[1]);

},
error: function (xhr) {
alert(xhr.responseText);
}
});

the data is coming from server. and working fine.

my requirement is to store msg.d in a variable and next time the data should display from stored variable.

likethis

var temp;
if (temp.length == 0) {
$.ajax({
type: "POST",
// url: "Busbooking.aspx/ConductorTable",
url: "Busdetails.aspx/BindData1",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
temp = msg.d;
var s = msg.d;
var k = s.split("--");

$('#disp').html(k[0]);
filter1();
$('#bus').html(k[1]);

},
error: function (xhr) {
alert(xhr.responseText);
}
});
}
else {
var s = temp;
var k = s.split("--");

$('#disp').html(k[0]);
filter1();
$('#bus').html(k[1]);
}


is it possible.

I tried the above code .but variable temp is showing as undefined

How to solve

Regards

Baiju




Responses

Posted by: Rajnilari2015 on: 10/22/2016 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
Instead of if (temp.length == 0) , try
if (temp == null)


which is equivalent to

if( typeof temp === 'undefined' || temp === null ){
..........................
..........................
...........................

}


--
Thanks & Regards,
RNA Team

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Manicse on: 10/26/2016 [Member] Bronze | Points: 25

Up
0
Down
You can also try like this,

if(temp != null)

{
if (temp.length == 0) {

--------------------- Other Conditions------------------

}
}


Without modifying your code. Just one If and else condition make this works

Mani.R

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response