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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:55:09+00:00 2026-05-27T19:55:09+00:00

I am trying to delete an item from a list. I am using knockout.js

  • 0

I am trying to delete an item from a list. I am using knockout.js with the mapping plugin. My code looks like this:

Serialize to Json

@{ var jsonData = new HtmlString(new JavaScriptSerializer().Serialize(Model));}

Template

<script type="text/html" id="imgsList">
    {{each model.Imgs}}
        <div style="float:left; margin: 10px 10px 10px 0;">
            <div><a href="${Filename}"><img src="${Filename}" style="width:100px;"></img></a></div>
            <div data-bind="click: deleteImage">Delete</div>
        </div>
    {{/each}}
</script>

K.O. JavaScript

<script type="text/javascript">
     $(function() {
        //KO Setup
        var viewModel = { 
            "model": ko.mapping.fromJS(@jsonData),
            "deleteImage" : function(item) {alert(item.Filename + ' deleted.');}
        }

        ko.applyBindings(viewModel);
    });
</script>

The HTML

<div data-bind="template: 'imgsList'"></div>

The Question

Everything works as expected. A list of images shows up with delete buttons, however, when you click a button item.Filename is undefined. Thoughts?

Edit: Taken from the KNockout.js Manual: “When calling your handler, Knockout will supply the current model value as the first parameter. This is particularly useful if you’re rendering some UI for each item in a collection, and you need to know which item’s UI was clicked.”

It appears that I am not getting back the Img object I am expecting. I don’t know what I am getting back!

  • 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-05-27T19:55:09+00:00Added an answer on May 27, 2026 at 7:55 pm

    When you use {{each}} syntax in jQuery Templates, the data context is whatever the overall template is bound against. In your case, that is the entire view model.

    A few options:

    1- you can use your current code and pass the item that you are “eaching” on to the function like ( http://jsfiddle.net/rniemeyer/qB9tp/1/ ):

    <div data-bind="click: function() { $root.deleteImage($value); }">Delete</div>
    

    Using an anomymous function in the data-bind is pretty ugly though. There are better options.

    2- you can use the foreach parameter of the template binding, which works with jQuery Templates and is more efficient than {{each}} like ( http://jsfiddle.net/rniemeyer/qB9tp/2/ ):

    <script type="text/html" id="imgsList">
        <div style="float:left; margin: 10px 10px 10px 0;">
            <div>
                <a href="${Filename}">${Filename}</a>
            </div>
            <div data-bind="click: $root.deleteImage">Delete</div>
        </div>
    </script>
    
    <div data-bind="template: { name: 'imgsList', foreach: model.Imgs }"></div>
    

    Now, the context of the template is the individual image object and calling $root.deleteImage will pass it as the first argument.

    3- Since, the jQuery Templates plugin is deprecated and Knockout now supports native templates, you might want to choose removing your dependency on the jQuery Templates plugin. You could still use a named template (just need to replace any jQuery Templates syntax with data-bind attributes) like: http://jsfiddle.net/rniemeyer/qB9tp/3/ or even remove the template and just go with the foreach control-flow binding like: http://jsfiddle.net/rniemeyer/qB9tp/4/

    <div data-bind="foreach: model.Imgs">
        <div style="float:left; margin: 10px 10px 10px 0;">
            <div>
                <a data-bind="text: Filename, attr: { href: Filename }"></a>
            </div>
            <div data-bind="click: $root.deleteImage">Delete</div>
        </div>
    </div>
    

    4- While I prefer option #3, you could even choose to use event delegation and attach a “live” handler like: http://jsfiddle.net/rniemeyer/qB9tp/5/

    $("#main").on("click", ".del", function() {
       var data = ko.dataFor(this);
       viewModel.deleteImage(data); 
    });
    

    This can be especially beneficial if you would be attaching a large number of the same handlers via the click binding (like in a grid).

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

Sidebar

Related Questions

SORRY FOR THIS CONFUSION: UPDATED QUESTION: I'm trying to remove a list item from
Somebody explain to me this: I am trying to delete items from a list
I'm trying to delete all digits from a string. However the next code deletes
To piggyback onto this question, PowerShell script to delete files from list and output
I am trying to delete the selected item from a ListView and the Problem
I am trying to implement ListView with Delete functionality to delete item from the
I keep getting this error when trying to delete a record from my table.
I am trying to delete several rows from a MySQL 5.0.45 database: delete from
I'm trying to delete a directory that contains XML files from a remote computer.
I am trying to delete an old user from our perforce installation. A previous

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.