I am able to load the jQuery library, but it seems that I can't call some simple jQuery function:
window.jQuery('.classText').attr('id');
I am trying to get element id by finding the element by class name. Using above script I always get undefined
. But using online playground like jsfiddle, I am able to get the element id
Here is the detail of my js and html file
ItemToApprove.js:
import { LightningElement, track } from 'lwc';
import { loadScript,loadStyle } from 'lightning/platformResourceLoader';
import jqueryPkg from '@salesforce/resourceUrl/jquerypackage';
export default class ItemToApprove extends LightningElement {
@track error;
dataTablejsInitialized = false;
renderedCallback() {
if (this.dataTablejsInitialized) {
return;
}
this.dataTablejsInitialized = true;
loadScript(this, jqueryPkg +'/jQuery-3.3.1/jquery-3.3.1.js')
.then(() => {
this.error = undefined;
//calling the jquery function
this.initializeTable();
})
.catch(error => {
this.error = error;
});
}
initializeTable() {
//always getting undefined instead of printing the element id
alert(window.jQuery('.classText').attr('id'));
}
}
ItemToApprove.html:
<template>
<lightning-card title="Item to Approve" icon-name="action:new_task">
<div class="slds-m-around_medium">
<input id="txtTest" class="classText" type="text" value="test" />
</div>
</lightning-card>
</template>
Basically I want to get the element using jQuery function by element class name. How can I achieve this in LWC?
window.
/document.
is not recommended in Web Components? LWC has its own way of doing DOM manipulations, why there is a need to use JQuery, is there any specific reason?window.
/document.
?this.chart = new window.Chart(ctx, this.config);