crea un elemento html con button radios in javascript

To create an HTML element with radio buttons using JavaScript, you can use the following code:

index.tsx
// create a div element
var div = document.createElement("div");

// create radio buttons
var radio1 = document.createElement("input");
radio1.type = "radio";
radio1.name = "radios";
radio1.value = "option1";

var radio2 = document.createElement("input");
radio2.type = "radio";
radio2.name = "radios";
radio2.value = "option2";

// create labels for the radio buttons
var label1 = document.createElement("label");
label1.innerHTML = "Option 1";

var label2 = document.createElement("label");
label2.innerHTML = "Option 2";

// append the radio buttons and labels to the div
div.appendChild(radio1);
div.appendChild(label1);
div.appendChild(radio2);
div.appendChild(label2);

// append the div to the document body
document.body.appendChild(div);
747 chars
30 lines

In the above code, we create a div element using document.createElement(). Then, we create two input elements with type set to "radio", and name set to "radios". We also set the value attribute of each radio button to "option1" and "option2", respectively.

Next, we create two label elements and set their innerHTML to "Option 1" and "Option 2", respectively.

Finally, we append the radio buttons and labels to the div element using the appendChild() method. We then append the div element to the document body using document.body.appendChild(). This will add the radio buttons to the page for the user to interact with.

gistlibby LogSnag