label not working with checkbox - html

OK, what am I missing?
I have:
<form>
<input type="checkbox" name="showRatings" value="1" checked>
<label for="showRatings">Show Ratings</label>
</form>
And when I click on the "Show Ratings" text, the checkbox is not toggling.
I know it's something simple.

I believe the label element links to the id attribute, not the name attribute. Try this:
<form>
<input type="checkbox" name="showRatings" id="showRatings" value="1" checked>
<label for="showRatings">Show Ratings</label>
</form>
Reference here.

When input element is inside the label then we do not need id on the element and 'for' attribute on the label, but when it is outside we need it.
<label>
Foo
<input name="foo" type="checkbox" />
</label>
Clicking on "Foo" will trigger a toggle of the checkbox

try this this will work. it will not work with name attribute.
<form>
<input type="checkbox" name="showRatings" id="showRatings" value="1" checked>
<label for="showRatings">Show Ratings</label>
</form>

Related

adding the radio button for html page

In my html page, I cant add two radio buttons, I get an error in free-code-camp and that is about to have two radio buttons in label element with attribute of same name value for both of them in input self-closing tag.
I tried the same value for name attribute in input tag within the label element. but I got error.
<label>
<input id="indoor" type="radio" name="indoor-outdoor" > Indoor
</label>
<label>
<input id="outdoor" type="radio" name="indoor-outdoor" > Outdoor
</label>
The following example shows three radio buttons with the same name, within a label, within a form.
This is a valid structure.
const handleSubmission = (form, event) => {
event.preventDefault();
console.log(`Choice: ${form.elements.foo.value}`);
};
<form onSubmit="handleSubmission(this, event)">
<label>
<strong>Choice:</strong>
<input type="radio" name="foo" value="a" /> A
<input type="radio" name="foo" value="b" /> B
<input type="radio" name="foo" value="c" /> C
</label>
<button type="submit">Submit</button>
</form>

How to add values in HTML forms?

How would i add the "value" that are selected from radio boxes in html forms? So when someone selects an option it would add the other "values" onto it and total that it at the bottom of the page. And does anyone know if it could add "names" total "values" onto it as well? thanks
My code looks like this:
<h3><u>Title</u></h3><br>
<form action="">
<input type="radio" name="num" value="0">Text<br>
<input type="radio" name="num" value="2">Text<br>
<input type="radio" name="num" value="80">Text<br>
<input type="radio" name="num" value="110">Text<br>
<input type="radio" name="num" value="85">Text<br>
<input type="radio" name="num" value="120">Text<br>
</form>
You cannot. By definition, a set of radio buttons with the same name attribute contributes at most one value to the data set, the one corresponding to the selected button.
If you want something else, you should handle that server side, or use other types of controls, or redesign the entire approach.
Working example :
(using a Javascript library, jQuery, but could be done in plain JavaScript)
You mainly have to change your inputs to type="checkbox" in the HTML
What code does : when a checkbox's state is modified, all checked checkboxes' value are summed up in the last input field I've added
The checkboxes are targetted by looking for "num" in their name, if you remove that the checkbox won't be taken into account by the script.
$(function() {
$("input[name*='num']").on("change", function() {
var total = 0;
$("input[type='checkbox']:checked").each(function() {
total += Number($(this).val());
});
$("#total").val(total);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h3>
<u>Title</u>
</h3>
<br>
<form action="">
<input type="checkbox" name="num0" value="0">Add 0<br>
<input type="checkbox" name="num2" value="2">Add 2<br>
<input type="checkbox" name="num80" value="80">Add 80<br>
<input type="checkbox" name="num110" value="110">Add 110<br>
<input type="checkbox" name="num85" value="85">Add 85<br>
<input type="checkbox" name="numwhateveryoulike" value="120">Add 120<br>
Total <input type="text" value="0" id="total">
</form>

Radio input checked

I have 2 radio inputs. A,B
I an trying to make A as a selected radio by default.
And I want the following behavior: when A get hidden then B should get checked by default.
This is what I tried. Please take a look.
<div >
<input type="radio" checked="checked" value="0" name="a" />
<strong>A</strong>
</div>
<div>
<input type="radio" checked="" value="1" name="b" />
<strong>
B
</strong>
</div>
<input type="text" onfocus="Javascript:document.forms[1].opt_payment[1].checked=true;" maxlength="20" value=" " name="" />
Thanks
Use the attribute checked="checked" for A. Also, if you want only one of A and B to be checked at a time, give them the same name but different values.
checked="" means "Should be checked by default". There is no way to have the attribute present but indicate "not checked".
If you don't want a form control to be checked by default then do not include any checked attribute for it
<div >
<input id="radioA" type="radio" checked="checked" value="0" name="a" />
<strong>A</strong>
</div>
<div>
<input id="radioB" type="radio" value="1" name="b" />
<strong>
B
</strong>
</div>
Script:
if($("#radioA").is(":hidden")){
$("#radioB").attr("checked","checked");
}
you can also use
$("#radioB").attr("checked",true);
or use prop for jquery version > 1.6
$("#radioB").prop("checked",true);

Payment options with radio buttons in the contact form

My form is currently set up to gather all the input data to my autoresponder...however, I made the form with only one option - pay now. Users would like options, so Im thinking of giving them 2 choices, the old "pay now" COD method, and option#2 paypal. I think radio buttons are the best way for doing this. However I cant get them to work separately...when I choose option 2, option 1 remains selected. So I added the radio buttons myself after the ordernow button.
<p>mail: *</p>
<p>
<label>
<input type="text" class="wf-input wf-req wf-valid__email" name="mail" class="mj" ></input>
</label>
</p>
<p>name: *</p>
<p>
<label>
<input type="text" class="wf-input wf-req wf-valid__required" name="name" class="mj" ></input>
</label>
</p>
<p>
<input type="submit" value="ORDER NOW" class="butt">
<div class="selectpaymentradios">
<label class="radio" >select payment</label>
<input class="radio" type="radio" name="cash" value="cash" checked /> <span>Ca$h</span>
<input class="radio" type="radio" name="ppal" value="ppal" /> <span>PaypaL</span>
</div>
<input type="hidden" name="webform_id" value="12x45"/>
</p>
</form>
<script type="text/javascript" src="http://xyz.com/view_webform.js?wid=12x45&mg_param1=1"></script>
Im trying to figure out how can I make this work with my autoresponder, I think this form has to be able to tell me what kind of payment did the customer chose...but the autoresponders form creator doesnt have radio buttons at all so Im stuck, I dont know if its possible...
<input class="radio" type="radio" name="cash" value="cash" checked /> <span>Ca$h</span>
<input class="radio" type="radio" name="ppal" value="ppal" /> <span>PaypaL</span>
the problem you hit, is very simple - you have to use the same name for all radio-buttons, where only one item should be selected. like this:
<input class="radio" type="radio" name="payment" value="cash" checked /> <span>Ca$h</span>
<input class="radio" type="radio" name="payment" value="ppal" /> <span>PaypaL</span>
The name attribute should be the same for both radio buttons:
<input class="radio" type="radio" name="method" value="cash" checked="checked" /> <span>Ca$h</span>
<input class="radio" type="radio" name="method" value="ppal" /> <span>PaypaL</span>
Also, if you are closing input tags, you are probably worried about XHTML validation. So instead of just checked you should type checked="checked".

marking a radio input as slected when page is loaded

I have two radio buttons. I want one of them to be shown as selected when the page loads. Which property I should use, and how?
Example:
<input type="radio" name="musictype2" value="rock" default> Rock<br>
<input type="radio" name="musictype2" value="alternative"> Alternative<br>
<input type="radio" name="test" value="2" checked="checked" />test
like this?
<input type="radio" name="genreselect" value="rock" checked="checked" />
<input type="radio" name="genreselect" value="alternative" />
In this case "rock" is preselected.
<form>
<input type="radio" name="genre" value="rock" checked /> Rock<br />
<input type="radio" name="genre" value="alternative" /> Alternative
</form>
This is the correct way. The name attribute needs to be the same in order for them to switch between.
Check it here.
This is how you use the checked attribute:
<input type=radio name="my_name" VALUE="my_value" checked />