How to access data object in a HTMLElement without jQuery

31
August 10, 2018, at 3:40 PM

In my web-application, I have binded jQuery keyboard to a textbox. And I want to call keyboard.close() function explicitly on the keyboard since I'm removing all the eventListeners to the textfield at the start.

I can call this function using jQuery, like below.

$('#chat-form').data('keyboard').close();

But I'm not allowed to use jQuery because of some memory issues. Is there anyway that I can access data object in a HTMLElement, without jQuery?

Answer 1

You can use as below;

var elem = document.getElementById('elId');
console.log(elem.dataset.<your-data-attribute-without-data-prefix>)
Answer 2

Found a solution!

jQuery returns the actual keyboard object when we call $('#chat-form').data('keyboard'). When we use document.getElementById(id-here).dataset.keyboard instead of $('#chat-form').data('keyboard'), it will return a string instead of an object.

So you have to cast the HTMLElement object first, as follows.

let kbElement = document.getElementById('chat-form').dataset.keyboard as any;

then you can call .close() function the kbElement variable as follows.

kbElement.close();
READ ALSO
How to trigger form submit / button click with onclick. For many different user IDs

How to trigger form submit / button click with onclick. For many different user IDs

Having a WordPress site with "givewp" donation plugin

20
How to print JSON rest api data on HTML list format by using typedata

How to print JSON rest api data on HTML list format by using typedata

I am using typescript for showing live search my JSON rest API data is showing but I want to show the data on list format but using for loop is will show only one data Here is my code :

31
Weird label on Flask Bootstrap search bar

Weird label on Flask Bootstrap search bar

For some reason I am not able to get rid of this "search" label on my search bar:

30
How to add custom parameter parameter in python functions?

How to add custom parameter parameter in python functions?

My name is Piyush and I have just started learning to code in pythonRight now I am working on a project

43