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 8930143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:49:07+00:00 2026-06-15T08:49:07+00:00

I would like to end up with the following HTML in the DOM using

  • 0

I would like to end up with the following HTML in the DOM using jQuery and KnockoutJs templates:

<div class="messageToAndFromOtherMember" id="13">
    <span>the message</span> 
    <span>2012-12-02 14:05:45.0</span> 
</div>

I have started writing my KO template as follows:

<div class="messageToAndFromOtherMember"  data-bind="attr:{ id: messageId}">
    <span data-bind="text: message"></span> 
    <span data-bind="text: sendDateFmted"></span> 
</div>

Upon a successful ajax request, the following js is executed:

var messageViewModel = {
         message: response.message, 
         sendDateFmted: response.sendDateFmted, 
         messageId: response.messageId
     };
ko.applyBindings(messageViewModel);

Now I am not sure how and where to actually do the binding: since my message does not exist before the ajax request is complete and I can have as several messages…

Can anyone please suggest a solution?

EDIT 1: I have added this to the html page:

<div data-bind="template: { name: 'message-template', data: messageViewModel }"></div>

I now get the following js error:

Uncaught Error: Unable to parse bindings. Message: ReferenceError:
$messageViewModel is not defined; Bindings value: template: { name:
‘message-template’, data: messageViewModel }

EDIT 2: I have altered my code as follows:

var messageViewModel = {
    data: ko.observable({   
        message: response.message, 
        sendDateFmted: response.sendDateFmted, 
        messageId: response.messageId
    })
     };

$("<div>",{
    class:"messageToAndFromOtherMember"
}).data("bind", "template: { name: 'message-template', data: data }").appendTo("#messagesToAndFromOtherMember");

ko.applyBindings(messageViewModel);

Please note that I am need to be able to add the element from jQuery in order to be able to add one message then another and so on. However, the above jQuery code does not work and I don’t see any such thing as data-bind in the DOM…

  • 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-15T08:49:08+00:00Added an answer on June 15, 2026 at 8:49 am
    var messageViewModel = {
        message: ko.observable(), 
        sendDateFmted: ko.observable(), 
        messageId: ko.observable()
    };
    ko.applyBindings(messageViewModel);
    

    When the data arrives from the server:

    messageViewModel.message(response.message);
    messageViewModel.sendDateFmted(response.sendDateFmted);
    messageViewModel.messageId(response.messageId);
    

    With the mapping plugin, you could do something like this instead:

    ko.mapping.fromJS(response, messageViewModel);
    

    You really don’t need templates, if you are not going to use it in multiple places. It is simpler without templates. Look at http://jsfiddle.net/3BuHR/

    If you are sure you need templates, here is an example: http://jsfiddle.net/Jxhm5/


    You seem to be using knockout just for it’s templating functionality. KO can do much more.

    I would instead let KO handle the DOM-creation based on observableArray‘s. When the array is modified, the DOM will be updated automatically.

    var viewModel = {
        messages: ko.observableArray(),
    };
    
    function Message(data) {
        data = data || {};
        this.message = ko.observable(data.message);
        this.sendDateFmted = ko.observable(data.sendDateFmted);
        this.messageId = ko.observable(data.messageId);
    }
    
    <div class="message-list" data-bind="foreach: messages">
        <div class="messageToAndFromOtherMember"  data-bind="attr:{ id: messageId}">
            <span data-bind="text: message"></span> 
            <span data-bind="text: sendDateFmted"></span> 
        </div>
    </div>
    

    And then when data arrives from the server:

    viewModel.messages.push(new Message(response));
    

    KO will track insertions, deletions, reorderings, and update the DOM for you.

    http://jsfiddle.net/v5sdf/

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

Sidebar

Related Questions

I would like to write a function using jQuery to append a div to
I am using jQuery datepicker, I have two datepicker input fields, like following: <html>
Given the following HTML fragment: <div class=word> <input type=text name=A /> <input type=text name=n
I succeed scrolling to top of my page using the following jQuery script: $(html,
I am using Rails 3.1.0 and I would like to understand why the following
I am using Ruby on Rails 3.0.9 and jQuery 1.6.2. I would like to
I have the following HTML on my page: <div id=mypanel> <span>SomeText</span> <span>SomeText2</span> <span>SomeText3</span> <span>SomeText4</span>
I would like to add a Javascript at the end of some ajax content,
I would like to remove/uninstall directX End-User that I downloaded and installed it from
I would like to search into directory for all files end by .m for

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.