I need help please in capturing the text data displayed by a webpage element. I can’t get it to work despite many hours of reading, trial and ultimately errors. I have added a colour picker to the nixie clock webserver dashboard I am building based on your webserver guide, so that I can adjust the clock’s settings without having to navigate its arcane tiny buttons process. I want to use the colour picker to set/change the colour of the LED backlighting. If I can capture the colour picker’s selected colour I can pass this to the nixie clock’s LED colour settings. This is the last dashboard element I am trying to build so its a bit frustrating hitting the wall.
The colour picker I am using is based on the https://github.com/bebraw/colorjoe library, that I learned about from https://www.youtube.com/watch?v=eGth988Sm8w
I have all that integrated and working fine on my evolving webserver project, but the guide I followed does not discuss capturing and using the “selected color” data, so it looks pretty but currently is not helping me.
The data I want is the hex code for the selected colour which is displayed and held by a class element called selected-color-text. I don’t know if this is helpful but it’s displayed in the console elements window as <div class = “selected-color-text”>#ff5812”</div> == $0 and its properties display include references to the hex code colour data I want to capture:
- innerHTML: #ff5812”
- innerText: #ff5812”
- outerHTML: “<div class:\”selected-color-text\”>#ff5812</div>
- outerText: #ff5812”
The HTML used to implement this in the YouTube guide is here: https://github.com/dcode-youtube/color-picker-with-local-storage
There is an example of saving color values from the color picker to local storage in this code, from a set of saved-color elements, but I can’t get a clone of this to work with the selected-color-text (or the selected-color element). Every way I have tried either gives errors or just doesn’t work. I can’t get a mouse eventListener to recognise the selected-color-text element so that is obviously a big problem right up front. This is my first serious project after working through your webserver book.
What I want to be able to do is click on the displayed hex code when I’m happy with the selection and capture it so that I can then send it to my clock. I know I could probably do this via the colours that the widget already saves from the selected-color now, but they are for a different purpose and that seems a bit untidy. I could also use a much simpler colour picker but I rather liked this fancier one. I’m assuming what I want to do is probably very simple for someone who knows how to do it? Hoping you can help please.
After some sleep and more searching and trialing, I have finally cracked it and can save the colour hex code to local storage.
Why does the mouse icon change its appearance on mouseover between the selected-color-text element and the selected-color element? What determines this? I assume they have different properties but I can’t see where they come from.
Don’t think I have mastered pastebin yet !? Looks OK there but not here??
const selColtxt = this.root.querySelector(“.selected-color-text”);
selColtxt.addEventListener(‘mouseup’, (e) => {
if (e.button == 0) {
localStorage.setItem(“colorpicker-text-selected”, JSON.stringify(this.selectedColor));
}
});
Hi.
To understand why that happens, check which HTML elements you have and what are they’re default properties.
I’m sorry, but I didn’t understand… Did you solve the problem?
Regards,
Sara
Thanks Sara. I’ll look into the ‘default’ properties.
I thought the pastebin processed code should have shown up formatted etc in the forum but its not. Should I have clicked embed in pastebin and pasted the link here instead?
<script src=”https://pastebin.com/embed_js/u4sQGsBm”></script>
Hi Sara
Why does the pastebin link get changed when submitted in the forum and then not work?
<script src=”https://pastebin.com/embed_js/u4sQGsBm”></script>
It is a valid link so what should I be doing differently with these so that they work in the forum?
I need help please. I’m still struggling.
I have added a colour picker to an RNT Lab ESP8266 webserver page I have built and I am wanting to send the color picker’s currently selected colour to an attached Arduino Mega2560, which my application is already exchanging data with successfully. This is to control the colour of my nixie clock LED backlights. The nixie clock is driven by an Arduino Mega2560.
The colour picker I am using is based on the https://github.com/bebraw/colorjoe library, that I learned about from https://www.youtube.com/watch?v=eGth988Sm8w The HTML used to implement this in the YouTube guide is here: https://github.com/dcode-youtube/color-picker-with-local-storage
I have the color picker integrated and working fine on my webserver project page and I can capture and save the hex code of the currently selected-color div element to js local storage with a mouse click, but I can’t figure out how to get that data item into my ESP8266 c++ main() program, so that I can send it to the Mega2560 via the serial port. I need this to control the colour of LED backlights.
Can I access js local storage from my c++ main() src or how else can I pass this information out of the colour picker js container to c++ so that I can send it via the serial port to control my nixie clock LED backlights?
Hi.
I’m sorry for taking so long to get back to you.
I’ve been out of the office with little access to the internet.
I’m not familiar with the color picker you’re using, but if you can save the data in a javascript variable, you can send it to the ESP8266 using websocket protocol, for example.
This is the simplest websocekt tutorial we have: https://randomnerdtutorials.com/esp8266-nodemcu-websocket-server-arduino/
Take a look at the javascript functions we use there. You can see that after initializing the WebSocket, we simply use websocket.send(YOUR_VARIABLE_HERE).
Then, look at the other websocket functions on the rest of the code. You get the message on the handleWebSocketMessage() function.
If you want a more simple solution, we have this tutorial that is a web server color picker: https://randomnerdtutorials.com/esp32-esp8266-rgb-led-strip-web-server/
We created that project some time ago, but I believe it is still working.
I hope this helps.
Regards,
Sara
Thank you Sara. No apology needed. I think you do an amazing job helping people and I am always very grateful to be able to seek your help. I did finally realise that a websocket message was the way to go, and just a few minutes ago I managed to get that working. The webserver I am using was already websocket based so I should have realised thst sooner. i understand a lot more about how it works now. I am now just sorting out the message handling as I have several other messages coming in too. As I can now see the selected-color information I wanted, on the server side through my sketch main() function on the ESP8226, I think my question is now resolved. No doubt I’ll encounter a few more hurdles before I am done. Again thanks for your guidance.