i have a js function that insert some code into exsiting html. HTML
<div id="filteredProducts">
<!--load by js-->
</div>
<div id="filteredplaces">
<!--load by js-->
</div>
<div class="container">
<div class="bucket">
</div>
</div>
js code
var myfun = function (data) {
if (data.form_is_valid) {
$("#filtered1").html(data.html);
toggleLight();
}
I need further abstract it to take another paramter so that i can insert data.html into any selector. So "#filtered1" will become a parameter pass into this function. For example (Not sure if it works)
var myfun = function (data, sel) {
if (data.form_is_valid) {
$(sel).html(data.html);
toggleLight();
}
Ideally i'd like to do following:
myfun(data,".container .bucket")
or
myfun(data,"#filteredplaces")
const myFunction = (data, selector) => {
if (data.isFormValid) {
$(selector).html(data.html);
// toggleLight();
}
};
myFunction({ isFormValid: true, html: '<h1>Hello</h1>'}, '.class1,.class2')
This seems to work as you intended. I just tested with the last line, and it does what you need.
Perhaps, you missed that you need a comma in between the classes when you are targeting multiple