Create radio button with javascript

I have an html table built from a DB. It has a β€œEdit” button that will open the form.

One of these input types will be the switch that I want to generate using the On / Off option:

<input type="radio" name="camera_status" value="On" /> On<br /> <input type="radio" name="camera_status" value="Off" /> Off 

I have 3 problems:

  • I do not know how to create several radio parameters. The code below only creates one.

  • How do I pass the value that I passed to the "camerastatus" default verified value?

  • I need to have some shortcuts. The form will be "Camera Status" and then the labels for turning on / off.

code:

 function edit2(to, cameraname, cameraquality, camerastatus, emailnotice, camerahash) { var mydiv = document.getElementById("editform"); var myForm = document.createElement("form"); myForm.method = "post"; myForm.action = to; //camera_status label = document.createElement("label"); label.for = "text"; label.innerHTML="<br>Camera status: <br>"; myForm.appendChild(label); var myRadioElement; try{ myRadioElement = document.createElement('<input type="radio" name="camera_status" />On'); }catch(err){ myRadioElement = document.createElement('input'); } myRadioElement.type = "radio"; myRadioElement.name = "camera_status"; myForm.appendChild(myRadioElement); mydiv.appendChild(myForm); } 
+4
source share
2 answers

To set the default switch, add a verified attribute.

 <input type="radio" name="..." checked /> 

Isn't it better to use a checkbox for what can be turned on or off?

 <input type="checkbox" ... /> 
+2
source

Checked "Male" value:

 <div><input type="radio" name="Gender" value="Male" checked="checked" /> Male</div> <div><input type="radio" name="Gender" value="Female" checked="" /> Female</div> 

To do this in JavaScript, you can simply put the code block where you need the radio buttons and use document.write with a for loop for each switch you need. (Of course, you also want to change the value every time).

-Neil

0
source

Source: https://habr.com/ru/post/1385040/


All Articles