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

  • SEARCH
  • Home
  • 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 8249309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:29:43+00:00 2026-06-07T23:29:43+00:00

I have a backbone.js View that reads a template from the HTML file and

  • 0

I have a backbone.js View that reads a template from the HTML file and inserts values from its model into the template. One of this value is in the variable title, which can be long enough to disrupt the flow of elements on the page. I want to use Javascript to limit the max. number of characters title can have, instead of doing it on the backend because eventually the full title has to be displayed.

I tried selecting the div that contains title after the template was been rendered, but cannot seem to select it. How can I do this otherwise?

Template

<script type="text/template" id="tpl_PhotoListItemView">
    <div class="photo_stats_title"><%= title %></div>
</script>

View

PhotoListItemView = Backbone.View.extend({
    tagNAme: 'div',
    className: 'photo_box',

    template: _.template( $('#tpl_PhotoListItemView').html() ),

    render: function() {
        $(this.el).html( this.template( this.model.toJSON() ) );
        console.log($(this.el).children('.photo_stats_title')); <!-- returns nothing -->
        this.limitChars();
        return this;
    },

    limitChars: function() {
        var shortTitle = $(this.el).children('.photo_stats_title').html().substring(0, 10);
        $(this.el .photo_stats_title).html(shortTitle);
    }
});
  • 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-07T23:29:45+00:00Added an answer on June 7, 2026 at 11:29 pm

    Rather than try to modify the title after rendering it, modify it as it’s rendered.

    Pass a maxlength variable to your template as well, then:

    <script type="text/template" id="tpl_PhotoListItemView">
        <div class="photo_stats_title"><%= title.substr(0,maxLength) %></div>
    </script>
    

    If title.length is less than maxlength, the entire string will display. If it’s greater, only the first maxlength characters will display.

    Alternatively, simply hardcode the maximum length of the title into the call to .substr()

    If you need to perform more advanced truncating (e.g. adding ‘…’ to truncated titles), you’re better off modifying the title before rendering the template, passing the truncated version of the title into the template

    Another option would be to override Model.parse(response), creating a shortTitle attribute on the model; this way it’s always available whenever you’re working with the model

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Backbone View that has a Collection as its Model. If the
I have a backbone.js/underscore.js template that I am feeding into a backbone view for
i have a backbone view that renders a model to screen. When the model
I have dowloaded backbone.js from http://documentcloud.github.com/backbone/ , included that file inside script tag and
I have backbone view that binds an action to an element in the DOM.
In my Backbone application I have a main view that shows previews of posts.
I have created a street view backbone view. The problem is that when shown,
I'm having an issue with backbone.js' models. I have a view that creates another
I have a standard list view setup with backbone that looks something like below.
I have two backbone Views. One is the main view and the other is

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.