Skip to main content
Announcements
See what Drew Clarke has to say about the Qlik Talend Cloud launch! READ THE BLOG
cancel
Showing results for 
Search instead for 
Did you mean: 
johannaalmm
Contributor II
Contributor II

$element not working to find ID of html-elements

Hello,

I'm developing one of my first extensions and it works as long as it is the only instance of said extension on the page. However, when I have multiple extensions on the same sheet, my eventlisteners start acting up. I've included the specific rows of code which confuses me:

$element.html(
'<li class="lui-list__item" id="myCheckbox">'+
'<input type="checkbox" id="myCheckbox"  class="myClass"/>'+
'<label for="myCheckbox" class="lui-list__text" id="myCheckboxLabel">abc'+
'<span class="lui-icon lui-icon--tick" aria-hidden="true">'+
'</label>'+
'</li>'+
'<li class="lui-list__item" id="otherCheckboxLi">'+
'<input type="checkbox" id="otherCheckbox" class="advancedCheckBoxes"/>'+
'<label for="otherCheckbox" class="lui-list__text" id="otherCheckboxLabel">cde'+
'<span class="lui-icon lui-icon--tick" aria-hidden="true">'+
'</label>'+
'</li>');

console.log($element.find("#myCheckbox"));
$element.find('#myCheckbox').on("change",function(e) {
console.log(layout.qInfo.qId)
if($element.find('#myCheckbox').checked == true) {
$element.find("#otherCheckboxLi").addClass("lui-disabled");
}
else {
$element.find("otherCheckboxLi").removeClass("lui-disabled");
}
 
});
 
When the paint methods is executed the first time, the first console.log prints out the individual checkbox of each extension. However, when I click the checkbox in the second extension, the action is performed in the first. In the console.log of the extensions ID, the ID of the first extension is printed, not of the one I clicked the checkbox in.
 
I thought using $element ensured that the code would operate within the specific instance of the extension, but am I missing something? How should I define the event listener I have bolded above in such way so it is applied in the specific instance of the extension?
Labels (2)
3 Replies
alex_colombo
Employee
Employee

Hi @johannaalmm one way for fixing this is to set a unique id to your HTML. Instead of using "myCheckBox" you could concatenate your visualization ID, something like "Gthye_myCheckBox". You can get visualization ID from layout object, under layout.qInfo.qId).

johannaalmm
Contributor II
Contributor II
Author

Hello @alex_colombo!

I have tried that, but no luck... Even with the included objectID, which is put together like:

$(""+layout.qInfo.qId+"myCheckBox").find('#myCheckbox').on("change",function(e) {});

alex_colombo
Employee
Employee

This is HTML stuff, not related to the extension itself. Please check your HTML code and see if you are pointing to the correct checkbox. 

Try to print into console $(""+layout.qInfo.qId+"myCheckBox").find('#myCheckbox') and check if you are selecting the correct HTML. Then, you can also try to use native JS intead of Jquery using addEventListener