0

My webpart is for SP16. I use code from this sample and when i click button in webpart, page is refreshed.

This is code for Create Item:

 private createItem(): void {
this.updateStatus('Creating item...');
pnp.sp.web.lists.getByTitle(this.properties.listName).items.add({
  'Title': `Item ${new Date()}`
}).then((result: ItemAddResult): void => {
  const item: IListItem = result.data as IListItem;
  this.updateStatus(`Item '${item.Title}' (ID: ${item.Id}) successfully created`);
}, (error: any): void => {
  this.updateStatus('Error while creating the item: ' + error);
});

}

HTML:

<button class="${styles.button} create-Button">
    <span class="${styles.label}">Create item</span>
</button>

Declare Event:

const webPart: SpFxOnPrenPnPjsWebPart = this;
this.domElement.querySelector('button.create-Button').addEventListener('click', () => { webPart.createItem(); });

I would like the page not to be reloaded when clicking a button? How do It?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.