1

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?

3
  • Using the 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? Oct 10, 2019 at 10:02
  • 1
    I'm trying to apply table into jQuery datatable, can you show me what is the best practice instead of using window. / document.?
    – Rival
    Oct 10, 2019 at 10:03
  • I'm following this example: github.com/trailheadapps/lwc-recipes/blob/master/force-app/main/…. I thought when we are using 3rd party library, we need to explicitly calling window. this.chart = new window.Chart(ctx, this.config);
    – Rival
    Oct 10, 2019 at 10:06

1 Answer 1

3

If you want to use jquery (or any 3rd party JS library), it is better to pass the native lightning selector to jquery - without which there is a chance you will run into problems with any of upgrades. In your case, you can use this.template.querySelector('.classText'). Below code will work:

window.jQuery(this.template.querySelector('.classText')).attr('id');

Below is full code:

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() {
    console.log('classText id => ', window.jQuery(this.template.querySelector('.classText')).attr('id'));
}

However, note that id will be modified at RUNTIME.

Don’t use ID selectors with querySelector. The IDs that you define in HTML templates may be transformed into globally unique values when the template is rendered. If you use an ID selector in JavaScript, it won’t match the transformed ID.

So, the id you get will be something like txtTest-101

1
  • 1
    Understood, we will not using the Id as reference. We just trying to figure out how to get the element. Thanks for pointing it out!
    – Rival
    Oct 10, 2019 at 10:45

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.