0

on my Master page I am applying the CSS to selected link in the accordion panel. But once I click the button page getting refreshed and I am loosing my CSS. is there anyway we can still keep the class applied on the selected link ( or Highlight the selected link) after the page refresh?

 $(document).ready(function () {
            $('#accordian li').click(function () {
                var href = $(this).addClass("active1").children("a").attr("href");
            });
});
2
  • You could change just parts of the page or use localStorage. Commented Apr 28, 2016 at 4:10
  • I need to achieve this functionality in asp.net mvc master page. Commented Apr 28, 2016 at 4:14

2 Answers 2

2

try this

    $(document).ready(function() {
var key = 'clickedHref';
      $("#clearButton").click(function() {
        localStorage.setItem(key, null);
      });

      $('#accordian li').click(function() {
    $("#accordian li").removeClass("active1");
        var href = $(this).addClass("active1").children("a").attr("href");
        localStorage.setItem(key, href);
      });

      var clickedHref = localStorage.getItem(key);
      if (clickedHref !== null) {
        var a = $('#accordian li a[href="' + clickedHref + '"]');
        if (a.length) {
          a.trigger("click");
        }
      }
    });

css:

.active1 a{
  font-weight:bold;
}

html:

<ul id="accordian">
    <li>
        <a href="?a=1">a1</a>
    </li>
    <li>
        <a href="?a=2">a2</a>
    </li>
</ul>
<button id='clearButton'>clear</button>

http://codepen.io/anon/pen/vGVjjP

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I am getting undefined after the page refresh.
-1

Using "preventDefault()" not solve your case?

$('#accordian li').click(function(e) {
e.preventDefault();
var href = $(this).addClass("active1").children("a").attr("href");
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.