I can addClass, toggleClass and removeClass using example code you provided.
How do I select and set the text content of the two classes "remote-button3x-on" and "remote-button3x-off" ?
w3s shows $("#test1").text("Hello world!"); which is not getting me anywhere.
I have added:
case "setContent":
$btn.text("Hello world!");
// $btn.toggleClass(payload.value); //this calls the jquery function by name (specified in .command) on the $btn and passes in .value
break;
but have no clue how to set content for class remote-button3x-on & remote-button3x-off.
hard to tell without seeing what kind of changes you have made in "script for all buttons with class remote-button" .
As it states, functionality deals with elements which have defined class remote-button. You have created an element with class remote-button3x, so it will be ignored. Also you want to change content of nested element which again has different class.
There is no magic.
W3s shows correctly - $("#test1").text("Hello world!") -> select element with id test1 and set it's text Hello world!
That is the basics.
In your case it should be -> select element with class remote-button3x-on and set it's text to Hello world!
Originally there was selector for class .remote-button and to find specific element, the data-topic must match with the msg.topic
var BUTTON_CLASS = ".remote-button";
...
var buttonSelector = BUTTON_CLASS + "[data-topic='" + msg.topic + "']"
So if your elements can not be selected, you'll need to adjust something. Even to make selector rules smarter or make your elements to follow the selector rules.
Thanks. I had already altered your code to target BUTTON_CLASS=.remote-button3x in place of .remote-button. Also all the places where you target buttontype=toggle I added equivalent code to target buttontype=checkbox and it is from here that I am stuck.
This is where I am now. I have tried lots of selector rules but cannot figure out how to change the content of these two spans ?
After that I need to figure out how to set the state. To isolate my attempts I have added:
case "setContent":
$btn.text("Hello world!");
// $btn.toggleClass(payload.value); //this calls the jquery function by name (specified in .command) on the $btn and passes in .value
break;
To identify what is the element you actually selected, do this. It logs out names classes for that element. If those are what you are looking for, you are good with selecting the element.
case "setContent":
console.log($btn.attr("class"));
break
And if that selected element is span, the $btn.text("Hello world!"); should work fine.
But you want to select what is marked with yellow. So the rules must be somehow different.
Again, the functionality is created for elements which are created in specific way. Your button is built differently. I can't still say what you need to change. Even how the button is built or the rules.
You can make the selector rule to be part of incoming payload.
Provide easy & efficient access to button text class state
Provide a level of user branding
Be persistent across browser refresh, node-red restarts/power failure.
Your initial example (of which I am truly grateful it has been incredibly illuminating) covered 1. completely ok,
2. all except text (at the beginning I did not have a styled button that exposed the on/off text, I only had an icon example). I am in the process of trying to style a button with both.
3. completely ok.
4. todo.
Can I confirm that this is all I need ?
msg is {"selector":".remote-button3x-on","command":"setContent","value":"HELLO"}
That gives you the option to target any element if you know the name of it's class or id. "selector":".remote-button3x-on" selects element with class remote-button3x-on "selector":"#my-element" selects element with id my-element
So basically you have freedom to select anything. What you will do with selected element, it's up to you. As far you are doing what is possible in terms of html, jquery, css ....
I see. It is no problem adding data-topic="10" to the span classes but... what ?
I suppose it is not so good if a user has to make many edits to "data-topic='x'" in order to make the instance unique.
Just a thing that is niggling me a bit. If I can select the button uniquely through
var buttonSelector = BUTTON_CLASS + "[data-topic='" + msg.topic + "']"
var $btn = $(buttonSelector);//get the button
Why can I not select from within the context of $btn the '.remote-button-3x-on' class text ?
Also I thought I saw a w3s example of something where if one had
class='class1-subclass1' and class='class1-subclass2' ...
that there was a group selector syntax like |- (read or) so what is returned are all elements whose class name ends with '-subclassX' but within 'class1' scope ?
I really struggle with my description sorry.
Is something like this possible:
If you can do this $(".remote-button3x-on", this) can I do something akin to
$(".remote-button3x-on", $btnscope) ?
At a cost I suppose. I am trying to avoid unnecessary uses of 'find' since I can predict the html structure so I tried to use something like $(this).children(".remote-button3x-on") where this is the $btn selected with BUTTON_CLASS + "[data-topic='" + msg.topic + "']"
But I get $(...).children is not a function in the console. Edit: Sorry found a typo. It actually returns undefined.