How to get parent form id using knockout.js?

by scotty_walker , in category: Javascript , a month ago

How to get parent form id using knockout.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by samara , a month ago

@scotty_walker 

You can get the parent form id using knockout.js by using the following code:

1
2
3
4
5
6
var viewModel = {
    parentFormId: ko.computed(function() {
        var parentForm = $(element).closest('form');
        return parentForm.attr('id');
    })
};


In this code, we are using a computed observable to find the closest form element to the current element (assuming you have a reference to the element in the element variable) and then getting its id attribute. The parentFormId property will then hold the id of the closest form element.