Changing css class in knockout.js on mouse click

Changing css class in knockout.js on mouse click

Asked on December 24, 2018 in knockout js.
Add Comment


  • 3 Answer(s)

        Make the click function based on altering the  observable variable .

        For instance :

    <div data-bind="css: { myclass: newClass() == true }">
        Profit Information
    </div>
    <button data-bind="click: changeClass">Change Class</button>
    <script type="text/javascript">
        var viewModel = {
            newClass: ko.observable(false),
            changeClass: function() {
                viewModel.newClass(true);
            }
        };
    </script>
    

        On the same element  joining both the click and css also possible . For example:

    <div databind="click: changeClass, css: { myclass: newClass() == true }"></div>
    
    Answered on December 25, 2018.
    Add Comment

    The answer is given in this  link: http://jsfiddle.net/ZmU6g/3/

    This problem also come in the  script tag .

    Answered on December 25, 2018.
    Add Comment

        The  attr binding  is highly used for the class binding purpose .

        This click binding is good for this to provide the answer that alter the  observable in the callback .

    Answered on December 25, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.