locked
Jquery addclass not working RRS feed

  • Question

  • User-712926555 posted

    Hi,

     <script type="text/javascript">
            $(document).ready(function () {
                var activeMenu = $('#hvmenu').val();
                $('#vmenu li a').removeClass('vActviceMenu');
                switch (activeMenu) {
                    case 'A1':
                        $('#A1').addClass('vActviceMenu');
                        break;
                    case 'A2':
                        $('#A2').addClass('vActviceMenu');
                        break;
                    case 'A3':
                        $('#A3').addClass('vActviceMenu');
                        break;
                    default: 
                }
            });
        
        </script>
     <ul id="vmenu" class="vmenu">
          <li><a id="A1" href="#" runat="server"> <i class="fa fa-angle-double-right"></i>&nbsp; Store </a> </li>
          <li><a id="A2" href="#" runat="server"> <i class="fa fa-angle-double-right"></i>&nbsp;Stock  </a></li>
          <li><a id="A3" href="#" runat="server"> <i class="fa fa-angle-double-right"></i>&nbsp; Update </a></li>
    </ul>

    A2, A3 working perfect. but A1 Not working. While be bugging everything was correct. But i dont know whats wrong with it?


    debugging

    Tuesday, September 15, 2015 2:08 AM

Answers

  • User-1667135504 posted

    Hi hisanth,

    You have never mention runat="server" before anywhere in your posted HTML .i.e UL , LI tag

    In asp.net if you add runat="server" attribute to any childpage control and if you have applied masterpage, then on client side your control id look like ContentPlaceHolder1_YOUR_CONTORL_ID. 

    IF you want to select element then you can do in 2 ways

    1) Use class instead of ID

    var value = $('.hvmenu').val();

    2) Use ClientID code in the aspx

    var value = $('#<%=hvmenu.ClientID%>').val();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 21, 2015 2:42 AM

All replies

  • User-1667135504 posted

    Here I made a demo on JsFiddle
    http://jsfiddle.net/4Lwjwuu6/1/ 

    And its working for A1,  You can not add activeMenu="A1" inside your switch case maybe that's the error you getting

    http://jsfiddle.net/4Lwjwuu6/1/

    Tuesday, September 15, 2015 3:40 AM
  • User1724605321 posted

    Hi Hisanth ,

    A2, A3 working perfect. but A1 Not working. While be bugging everything was correct. But i dont know whats wrong with it?

    I test your code in my environment and it works fine . I would suggest you could use F12 Developer Tools to check whether the "A1" tag has achieved the css class successfully and not be covered by other css style .

    Best Regards,

    Nan Yu

    Tuesday, September 15, 2015 4:51 AM
  • User-474980206 posted

    most likely you have another element with the id "A1". in the debugger test what the selector returns:

       $('#A1')

    Thursday, September 17, 2015 11:23 AM
  • User-712926555 posted

    If i am removing  runat="server" It works perfect. How to solve it with  runat="server"

    Monday, September 21, 2015 2:04 AM
  • User1724605321 posted

    Hi Hisanth ,

    How to solve it with  runat="server"

    Where you have the tag and please explain more about the problem you have met .

    Best Regards,

    Nan Yu

    Monday, September 21, 2015 2:16 AM
  • User-1667135504 posted

    Hi hisanth,

    You have never mention runat="server" before anywhere in your posted HTML .i.e UL , LI tag

    In asp.net if you add runat="server" attribute to any childpage control and if you have applied masterpage, then on client side your control id look like ContentPlaceHolder1_YOUR_CONTORL_ID. 

    IF you want to select element then you can do in 2 ways

    1) Use class instead of ID

    var value = $('.hvmenu').val();

    2) Use ClientID code in the aspx

    var value = $('#<%=hvmenu.ClientID%>').val();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 21, 2015 2:42 AM