0

I'm using asp.net web api project and using jquery template in that.

in ready
$('a').click(function () {
            debugger;
            // remove the selected class from all anchors
            $('.row a').removeClass('selected');

            // Add the selected class to the currently clicked anchor
             $(this).addClass('selected');            
        });

<div class="row">
            <div class="span3">
                <!-- Added dynamic data from script GroupTypeTemplate -->              
            </div>
</div>
<script id="GroupTypeTemplate" type="text/html">
                <nav id="options" class="work-nav">
                    <ul id="filters" class="option-set" data-option-key="filter">                                            
                         <li>                            
 <a onclick="getGroupById('${Id}')"  data-option-value="*">${TypeName}</a>
                         </li>
                    </ul>
                </nav>                 
                </script>

I want to set that class attribute dynamically as per which menu is currently active

But how to check which id is currently active accordingly I can set class="selected"

What is the syntax in jquery template ?

2
  • 1
    check which menu is active and change class accordingly Commented Jan 21, 2014 at 19:02
  • sorry to ask you but i'm new to jquery template where and what is the syntax to do so? Commented Jan 21, 2014 at 19:03

2 Answers 2

1

this will put the selected attr only to the a that was clicked.

$("a").click(function(){
    $("a").removeClass("selected");
    $(this).addClass("selected");
});
Sign up to request clarification or add additional context in comments.

2 Comments

Is your <li><a></a><li> generated dinamically? try to put this inside $(document).ready(function(){ PUTHERE });
then try to use $("a").on('click', function(){ $("a").removeClass("selected"); $(this).addClass("selected"); }); instead.
0

to get script to work on dynamically added content you need to put the event on the document like this

$(document).on('click', 'a', function(){
    //remove, add here
});

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.