Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toggle HTML radio button by clicking its label

Is there please a simple way to make a radio button toggle - when a text near it is clicked - without introducing any big Javascript Framework into my smal PHP project?

The web form looks like this:

<html>
<body>
<form method="post">
<p>Mode:<br /> 
<input type="radio" name="mode" value="create"><i>create table</i><br />
<input type="radio" name="mode" value="select" checked>select records (can specify id)<br />
<input type="radio" name="mode" value="insert">insert 1 record (must specify all)<br />
<input type="radio" name="mode" value="delete">delete records (must specify id)<br />
<input type="radio" name="mode" value="drop"><i>drop table</i><br />
</p>
<p>Id: <input type="text" name="id" size=32 maxlength=32 /> (32 hex chars)</p>

<p>Latitude: <input type="text" name="lat" size=10 /> (between -90 and 90)</p>
<p>Longitude: <input type="text" name="lng" size=10 /> (between -90 and 90)</p>
<p>Speed: <input type="text" name="spd" size=10 /> (not negative)</p>
<p><input type="submit" value="OK" /></p>
</form>
</body>
</html>
like image 717
Alexander Farber Avatar asked Nov 18 '10 09:11

Alexander Farber


People also ask

How do I select a radio button when a label is clicked?

We can do this by providing the radio's ID attribute value in the for attribute of the <label> element. Now, you can click the associated label to select the corresponding radio button. If your radio button doesn't have an id attribute, you can wrap the radio button directly inside the <label> element.

How do I link a radio button to a label in HTML?

To label a radio button, add a <label> element after the <input> element and insert a for attribute with the same value as the id of the associated <input> element. Then, write your label text in the <label> tag.

Can radio buttons have OnClick?

Each RadioButton has been assigned a JavaScript OnClick event handler. Note: The RadioButtons must be set with exact same name attribute values in order to make them mutually exclusive. When the RadioButton is clicked, the ShowHideDiv JavaScript function is executed.

How do I automatically select a radio button in HTML?

You can check a radio button by default by adding the checked HTML attribute to the <input> element. You can disable a radio button by adding the disabled HTML attribute to both the <label> and the <input> .


4 Answers

You can also wrap your elements without giving them each an ID, since a <label> is implicitly for the input it contains, like this:

<label>
   <input type="radio" name="mode" value="create">
   <i>create table</i>
</label>
like image 135
Nick Craver Avatar answered Nov 15 '22 12:11

Nick Craver


You can use <label> elements, which are designed to do exactly that:

<input type="radio" id="radCreateMode" name="mode" value="create" />
<label for="radCreateMode"><i>create table</i></label>
like image 22
Frédéric Hamidi Avatar answered Nov 15 '22 12:11

Frédéric Hamidi


Wrapping the input under the label should work.

<label>
    <input type="radio" name="mode" value="create"><i>create table</i>
</label>
like image 33
Jason Ching Avatar answered Nov 15 '22 13:11

Jason Ching


I had trouble finding the value of the radio button using this method, an alternative is to use a hit area increasing clickable area of a button.

like image 22
Christopher Grigg Avatar answered Nov 15 '22 14:11

Christopher Grigg