From my code I have a function that can create div elements multiple times (with surroundContents()), how can I make these created div elements to trigger a function inside the class of the very same page?
More precisely, I need that any of these div elements (created with function createDiv()) be able to trigger with click the function called clickCreatedDiv(arg) located in MyPage class and pass any string as an argument. I have tried this element.setAttribute('(click)', 'clickCreatedDiv()'); but with no success. How can I achieve this behavior?
export class MyProvider {
wrapSelection(comment) {
let element = document.createElement("div")
element.setAttribute('(click)', 'clickCreatedDiv()');
element.surroundContents(element);
}
}
import { MyProvider } from '../../providers/my-provider';
@Component({
selector: 'page-mypage',
templateUrl: 'mypage.html',
})
export class MyPage {
constructor(public myprovider: MyProvider) {}
createDiv() {
this.myprovider.wrapSelection();
}
clickCreatedDiv(arg) { // This is what I want to trigger with click
alert('click ' + arg);
}
}
setAttribute('onclick', 'clickCreatedDiv()')onclickworks, now you have to reach the function. ProbablyMyPage.clickCreatedDiv()would work?onclickattribute function should be available from the global scope. Otherwise it would be better to set the event handler in that scope whereclickCreatedDivis available.clickCreateDiv? Is it thecommentparameter ofwrapSelection(comment)?