Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9189415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:13:54+00:00 2026-06-17T20:13:54+00:00

I have a Backbone template that looks like this: <script type=text/html id=template-switchers> <strong><%= activeViz

  • 0

I have a Backbone template that looks like this:

<script type="text/html" id="template-switchers">
<strong><%= activeViz %></strong>
<button class="btn" data-viz="lists"><i class="icon-list icon-large"></i></button>
<button class="btn" data-viz="atomic"><i class="icon-spinner icon-large"></i></button>
<button class="btn" data-viz="maps"><i class="icon-map-marker icon-large"></i></button>
<button class="btn" data-viz="charts"><i class="icon-bar-chart icon-large"></i></button>
</script>

The activeViz parameter passed in can be any of lists, atomic, maps and charts. My question is: how do I add an active class to the appropriate button?

To date, I’ve been doing this with jQuery:

$('#viz-switchers button[data-viz="'+ activeViz + '"]').addClass('active');

But now I want to refactor for Backbone, and use templates. Is there a better, Backboney way, without having four if statements in the template?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:13:55+00:00Added an answer on June 17, 2026 at 8:13 pm

    You can create array of buttons and render it in loop e.g:

    var buttons = [{viz:'', icon:''}, {}, {}];
    

    And In your template render buttons:

    <% _.each(buttons, function(button) { %>
        <button class="btn" data-viz="<%= button.viz %>">
            <i class="<%= button.icon %> icon-large <% button.viz === activeViz && print('active') %>"></i>
        </button>
    <% }) %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a working coffeescript/backbone idiom that looks like this: SidebarWidgets = ((() ->
I have a backbone.js View that reads a template from the HTML file and
I have the following template in Backbone, all gets rendered perfectly. <a href=#list data-role=button
I am using underscore.js's templating capabilities from backbone.js, I have the following template that
I'm using backbone for an app that I'm building. In this app, I have
I have a backbone.js/underscore.js template that I am feeding into a backbone view for
I have a backbone fetch view method that does this: var ResultsView = Backbone.View.extend({
I have created a Backbone.js/Require.js application that dynamically loads HTML templates to use as
I have Backbone.js collection that holds (for example) 30 items. I want to pass
I have backbone view that binds an action to an element in the DOM.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.