I am trying to create a radio group and since it spans over several line, with images in between I cannot use sp-radio-group. I tried and it did not work.
I add the name= attribute to link all groups together.
I do not want to use to onclick event, I will check this when submitting the form.
The problem is that:
the selected item, if it is selected at all, shows fait blue circle and
It gets deselected as soon as i press any other button or the panel loose focus.
What is wrong with it?
Can I use sp-radio-group across multiple divs?
I managed to do something you wanted to achieve. I did it in React but you will get the point. Maybe you can use the SVG icons, and with some CSS tricks like in this code, I did you will get what you need. As far as I know, the recommendation is to use sp- components instead of native web elements as these are going to be deprecated in the future.
I decided, until adobe fix this, to wrap the radio button in ab , add some css and it is working. I need to remember to press a little bit outside the button though.
If you use the code that I sent you you could click on the image (SVG code) or the radio button (the small circle) and it would activate the selected option. As I saw when I used that kitchen sink sample code, the behavior of the sp-radio-button is better than the native HTML element. You don’t need to be so precise with the mouse in order to select the radio button you want.
Do you know how to get the selected radio value? I don’t want to use an event listener - I want to check the value before sending.
I used to do: $('input[name=which_V]:checked', '#preset_V').val();
I could iterate over all the radio buttons but is there a more elegant way with sp-radio-group?
The most elegant solution is to add a single event listener to the sp-radio-group and then listen to a “change” event. Because that group has a “name” property and only a single radio button can be selected at a time it will give you only the selected option.
Well, you can do a for loop if you want to. I think it’s less code and more elegant to have one event listener that is added just for an sp-radio-group element like this:
// Add event listener for a checked option
document
.querySelector(".radio-buttons")
.addEventListener("change", (e) =>
console.log(`Selected item: ${e.target.value}`)
);
That class name radio-buttons is the class for the sp-radio-group element. But you can do a for loop if you prefer it as long as it works
And it’s important to use and set that name attribute for the sp-radio-group element so that all radio buttons are grouped under the same name and only a single radio button can be selected at the time.
I will, however, it is working without the name in the group.
I think the name is not supported (remember reading abut it in one of the question in this forum).
I thing everything in a group is under the same name
Yes. I saw they didn’t list it on the official UXP documentation but at the same time, it can be found in the “kitchen sink” sample code.
I guess it won’t hurt to have it but it can work without it as well. That event listener can be attached to an element with that specific class name (like in this example .radio-buttons) and it should work on “change” event like they showed it on the official documentation: