How to bind the current value of an attribute image in a ViewModel using the tag img (Knockout.js)?


User 3533435

How to bind the current value of an attribute image in a ViewModel using the tag img (Knockout.js)? I want to see the current value (eg, 'sort_down.png'), but the attribute src of the image tag doesn't change ('no_sort.png'). Help me to solve the problem in this code. I also used the Knockout method ko.computed but the code doesn't work.

<img data-bind="attr:{src: image}"></p>
<div data-bind="click: sortTableByName"></div>

function ViewModel() {

                var self = this;

                self.articles = ko.observableArray(model);

                self.asc_sort_string = ko.observable(false);
                self.desc_sort_string = ko.observable(false);

                self.image = ko.observable('no_sort.png');

                self.update = function(){
                    // ... code
                };

                self.sortTableByName = function(){
                    if (self.desc_sort_string == false) {
                       self.asc_sort_string = false;
                       self.desc_sort_string = true;
                       self.image = 'sort_down.png';
                    }else{
                       self.asc_sort_string = true;
                       self.desc_sort_string = false;
                       self.image = 'sort_up.png';
                    }
                };


            };

            var viewModel = new ViewModel();

            ko.applyBindings(viewModel);

            viewModel.update();
Catalina

You define properties as observables and you need to update them accordingly.

self.sortTableByName = function(){
    if (self.desc_sort_string() == false) {
       self.asc_sort_string(false);
       self.desc_sort_string(true);
       self.image('sort_down.png');
    }else{
       self.asc_sort_string(true);
       self.desc_sort_string(false);
       self.image('sort_up.png');
    }
};

Related


Knockout js bind base64 image to image tag

Katie Savage I'm trying to bind an image to an HTML image tag using knockout js, it's not working, I have the base64 string in the database and I'm getting it from the database, but what's not working for me below is my code //viewModel// function ViewModel

Knockout js bind base64 image to image tag

Katie Savage I'm trying to bind an image to an HTML image tag using knockout js, it's not working, I have the base64 string in the database and I'm getting it from the database, but what's not working for me below is my code //viewModel// function ViewModel

Knockout js bind base64 image to image tag

Kitty Sarvaj I'm trying to bind an image to a HTML image tag using knockout js, it's not working, I have the base64 string in the database and I'm getting it from the database, but what's not working for me below is my code //viewModel// function ViewModel(

Knockout js bind base64 image to image tag

Katie Savage I'm trying to bind an image to an HTML image tag using knockout js, it's not working, I have the base64 string in the database, I'm getting it from the database, but what's not working for me below is my code //viewModel// function ViewModel(da

Knockout js bind base64 image to image tag

Katie Savage I'm trying to bind an image to an HTML image tag using knockout js, it's not working, I have the base64 string in the database, I'm getting it from the database, but what's not working for me below is my code //viewModel// function ViewModel(da

Bind list to Viewmodel using knockout

a newcomer I am new to MVC and Knockout and JS. I am trying to display a list of providers using knockout. I have the following code to get a list of providers public ActionResult Index() { Provider providerList = new Provider();

Bind list to Viewmodel using knockout

a newcomer I am new to MVC and Knockout and JS. I am trying to display a list of providers using knockout. I have the following code to get a list of providers public ActionResult Index() { Provider providerList = new Provider();

How to get the value of alt attribute of img tag?

Vivek Pipaliya I can't get alt attribute value of img tag The following is to get the value of the attribute alt of the img tag <html> <head> <script> $('img').click(function () { var alt = $(this).attr("alt") alert(alt); }); </script> </head> <img

How to get the value of alt attribute of img tag?

Vivek Pipaliya I can't get alt attribute value of img tag The following is to get the value of the attribute alt of the img tag <html> <head> <script> $('img').click(function () { var alt = $(this).attr("alt") alert(alt); }); </script> </head> <img

How to get image source attribute value from img html tag in c#?

Goofy My string variable stores a set of HTML tags along with a link to the img source file. string str = "<div> <img src="https://i.testimg.com/images/g/test/s-l400.jpg" style="width: 100%;"> <div>Test</div> </div>" How to get src attribute value from string

How to get image source attribute value from img html tag in c#?

Goofy My string variable stores a set of HTML tags along with a link to the img source file. string str = "<div> <img src="https://i.testimg.com/images/g/test/s-l400.jpg" style="width: 100%;"> <div>Test</div> </div>" How to get src attribute value from string

Insert an image into the current img tag

Maleron I am generating cats. What happens now is that each time generateCat()it runs, the cats-containerdiv is cleared and then the new tag is imginserted . I believe this is a very slow approach, is there any other way to "replace" these srcs without having

Knockout JS binding doesn't output value using simple viewmodel

Carl Weiss I'm trying to load a simple view model that has two models: Profile (a ko.observable()) and Request (a ko.observableArray()). When I apply the binding and then try to bind to the ether profile.field or foreach: request, I get no output, no javascrip

Unable to bind viewmodel in collection using Knockout

Sachin Gaur I want to bind a viewmodel item to a part of HTML in a collection that is bound to another HTML in the page. Here is my sample HTML and JS code: <div> <div style="width: 200px; float: left;" class="roundedBorder"> <fieldset id="fieldsetCategory

How to bind Knockout viewModel only for available keys?

Mayank gupta I'm data-bindusing .cshtml to dynamically create knockout properties in MVC. I just want to bind those properties that are available in the viewModel, which again I create dynamically from the results of static WCF. Therefore, there may or may be

How to bind Knockout viewModel only for available keys?

Mayank gupta I'm data-bindusing .cshtml to dynamically create knockout properties in MVC. I just want to bind those properties that are available in the viewModel, which again I create dynamically from the results of static WCF. Therefore, there may or may be