I want to know is it possible to convert this piece of jquery code to javascript
var port = chrome.runtime.connect({name: "SNT"});
$(document).ready( function() {
chrome.storage.local.get('SNT', function(items) {
$("#SNT").text(items.SNT);
});
$("#SNT").bind('keyup', function() {
chrome.storage.local.set({ 'SNT': $(this).val() }, function(){});
});
});
for your information the SNT is the Id of a textarea :
<textarea id="SNT"></textarea>
Thank you in advance
mahdiweb:
$(document).ready(
document.addEventListener('DOMContentLoaded',...
mahdiweb:
$("#SNT")
document.getElementByID
mahdiweb:
.text(
.innerHTML
mahdiweb:
.bind(
.addEventListener
mahdiweb:
$(this)
e.eventTarget
or this
mahdiweb:
.val()
.value
5 Likes
hello and thank you my dear friend @m_hutley hope you are well,
I modified the code as follows:
var port = chrome.runtime.connect({name: "SNT"});
document.addEventListener('DOMContentLoaded', () => {
chrome.storage.local.get('SNT', function(items) {
document.getElementById("SNT").innerHTML = items.SNT;
});
document.getElementById("SNT").addEventListener("keyup", function(){
chrome.storage.local.set({ 'SNT': this.innerHTML }, function(){});
},false);
});
when i test the code it doesnât show me any error message but it doesnât work i donât know why i think something is missing
Does the code with jQuery code work?
If it does, then gradually replace parts of jQuery with vanilla JavaScript, until you find the part that causes it to stop working.
1 Like
itâs good thank you very much, it works.
1 Like
hi @m_hutley ,
sorry for the disturbance but I have another small problem if you can guide me please,
I have the value âundefinedâ on the Textarea field with the following code
document.addEventListener(âDOMContentLoadedâ, () => {
chrome.storage.local.get(âSNTâ, function(items) {
document.getElementById(âSNTâ).innerHTML = items.SNT;
});
document.getElementById(âSNTâ).addEventListener(âchangeâ, function(e){
chrome.storage.local.set({ âSNTâ: e.target.value}, function(){});
},false);
});
I tried to put it in a function but it doesnât work
Why do you use chrome.storage? What about users using other browsers?
chome and the browser allowed where I work
You have undefined⌠where, specifically?
About the only part of the code that could result in that is the following:
I would hazard a guess that the textarea field has an identifier of SNT.
well thats pulling from the items setup, so if OP is getting undefined there, the problem isnt in the jquery->javascript transformation, its in their storage system
That said, OP didnt use my translations correctlyâŚ
m_hutley:
mahdiweb:
.val()
.value
2 Likes
mahdiweb:
this.innerHTML
Hi @m_hutley ,
The code you gave me works very well without any error but the only problem is the âundefinedâ error that appears on the textarea at the first launch of the extension and that I have to delete to write.
Well thatâs a logical error in your code here. You dont check to see if items.SNT exists.
1 Like
Hi @m_hutley ,
Perfect, really I donât know how to thank you, you are very kind thank you very much.
1 Like
system
Closed
January 23, 2023, 7:25pm
16
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.