0

I have a jquery slider that open and close my div. but when the page get a postback the div is closed. and i want the div to stay open.

my slider

 if ($(".div").is(":hidden")) {
            $(".div").slideDown(1000); // slide it down

        } else {
            $(".div").slideUp(1000); // hide it
        }

I thought about change the address, such as

website.com#open

and then check the adress but how can i do this ?

saadan
  • 467
  • 1
  • 10
  • 17

4 Answers4

0

When the page postbacks, the markup reverts to the original state. So, $(".div").is(":hidden") will always return true.

You can create some sort of state identifier and use that to check the state.

Starx
  • 77,474
  • 47
  • 185
  • 261
0

I would handle this in Html. You must have written the HTML statement this way

<div class="div" style="display:none"></div>

I would write:

<div class="div"></div>
Ulhas Tuscano
  • 5,502
  • 14
  • 58
  • 89
  • when i got multiple divs and i want only the one the user opened to be open then i cant do this. because then alle the divs will open. but thanks for the answer – saadan Apr 16 '12 at 10:40
0

try to do it this way:

 var isPostBackObject = document.getElementById('isPostBack');
 if (isPostBackObject != null) {

 } else {
 //show your div open
 }
coder
  • 13,002
  • 31
  • 112
  • 214
  • actually i have tried somthing like this. but when I have several divs which can slide on my page I dont know which one I have opened after a postback. if you understand. if i only got one i could do this but i got more than one that can slide on the same site – saadan Apr 16 '12 at 10:07
  • @saadan-each div should have a seperate "id" based on that you can do.Or else are you giving the same id to all div's?And if you have all div's contained in a single div that will make you to work easy way so that you can make that div visible so that every other div will be open on page load. – coder Apr 16 '12 at 10:08
  • sure all my divs got a unique id but in the if (isPostBackObject != null) i dont know wich div to open ? because I can not see which user opened – saadan Apr 16 '12 at 10:25
  • you can have a unique classname but not the ID's.try with the classname. – coder Apr 16 '12 at 10:27
  • still i dont know wich one the user opened. – saadan Apr 16 '12 at 10:38
  • if you need only one div to be opened why dont you give different ID's to each individual div and why are you making yourself complicated with the same ID's and confusing. – coder Apr 16 '12 at 10:41
  • dont think you know what i mean. and sorry i dont think i can explain it better in English. – saadan Apr 16 '12 at 19:05
  • @saadan-No need of sorry but your problem should solve.If you post a http://jsfiddle.net/ demo or a live link to your website that will be helpful so that I can see what's actually going on and further I can help you on that. – coder Apr 16 '12 at 20:01
0

You need to persist the state of the div between postbacks. You could do this either by passing state between your postbacks in the url or else recording state on the server in preparation of the next postback.

You could use a url with a param such as;

http://website.com?open=true

You could then check for the div state on the page loading using a url param parsing function such as;

var QueryString = function () {
  // This function is anonymous, is executed immediately and 
  // the return value is assigned to QueryString!
  var query_string = {};
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
        // If first entry with this name
    if (typeof query_string[pair[0]] === "undefined") {
      query_string[pair[0]] = pair[1];
        // If second entry with this name
    } else if (typeof query_string[pair[0]] === "string") {
      var arr = [ query_string[pair[0]], pair[1] ];
      query_string[pair[0]] = arr;
        // If third or later entry with this name
    } else {
      query_string[pair[0]].push(pair[1]);
    }
  } 
    return query_string;
} ();

You could then check on page load using:

$(document).ready(function(e){
if(QueryString.open == 'true')
{
 $(".div").show();
}
});

javascript param parse function written by Quentin here.

Community
  • 1
  • 1
Brian Scott
  • 9,221
  • 6
  • 47
  • 68
  • many thanks for the answer you're closer to what I'm looking for but how do I change my query when I press my button. ? – saadan Apr 16 '12 at 10:35
  • @saadan, it depends on what your button does and whether it is in a form as a submit button. Assuming it is, ensure that your form "action" url is updated to reflect the current state of the div so that after the page refreshes the url param is present. – Brian Scott Apr 16 '12 at 14:15
  • when i press the button it run the jquery slider so I cant make the page refresh. for otherwise it will not run the slid function. i have to set it on client side. – saadan Apr 16 '12 at 19:04
  • @saadan: You must understand that you cannot manipulate the url without changing the browser location or posting a form. The best approach for you would be to set a cookie value each time you change the state of the div slider. That way you can change the cookie value each time and the slider state will then be present between page postbacks. – Brian Scott Apr 17 '12 at 07:29