Radio input not changing value? - html

I have a radio input that is not changing values when selected. It is simply saying Domain is the value all the time.
<div class="col-xs-2">
<label class="radio-inline" for="AccountType_Domain"> <input id="AccountType_Domain" name="AccountType" type="radio" value="Domain">Domain</label>
<label class="radio-inline" for="AccountType_Local"> <input id="AccountType_Local" name="AccountType" type="radio" value="Local">Local</label>
</div>
At first I was using #html.RadioButtonFor but that was causing the 2 inputs to have the same id.
When executing $("input:radio[AccountType]").val() I am only getting Domain no matter what is selected.

You have to use the :checked css selector.
So, to get the value of the selected AccountType item in jQuery, it will look as follows:
$('input[name=AccountType]:checked').val()

You need to use:
$("input[name='AccountType']:checked").val()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="col-xs-2">
<label class="radio-inline" for="AccountType_Domain"> <input id="AccountType_Domain" name="AccountType" type="radio" value="Domain" checked>Domain</label>
<label class="radio-inline" for="AccountType_Local"> <input id="AccountType_Local" name="AccountType" type="radio" value="Local">Local</label>
</div>

Related

Radio button text click result wrong answer [duplicate]

This question already has an answer here:
2 sets of radio buttons with same IDs
(1 answer)
Closed 10 months ago.
When I click the text of "prefer not to say" of the second and third questions, the first question's answer changes to "prefer not to say". When I click the text of 'other' of the third question, the second question's answer changes to 'other'. In both cases, the third question's 'other' and 'prefer not to say' do not check when I click text. What is wrong with this code?
It runs well when I click the radio button.
<div>
<p><strong>1. What is your age?</strong></p>
<input type="radio" id="Under18" name="h11" value="Under18">
<label for="Under18">Under 18</label><br>
<input type="radio" id="18-25" name="h11" value="18-25">
<label for="18-25">18 - 25</label><br>
<input type="radio" id="26-35" name="h11" value="26-35">
<label for="26-35">26 - 35</label><br>
<input type="radio" id="46-55" name="h11" value="46-55">
<label for="46-55">46 - 55</label><br>
<input type="radio" id="Over55" name="h11" value="Over55">
<label for="Over55">Over 55</label><br>
<input type="radio" id="Prefer not to say" name="h11" value="Prefer not to say">
<label for="Prefer not to say">Prefer not to say</label>
</div>
<div>
<p><strong>2. What is your gender?</strong></p>
<input type="radio" id="Female" name="h12" value="Female">
<label for="Female">Female</label><br>
<input type="radio" id="Male" name="h12" value="Male">
<label for="Male">Male</label><br>
<input type="radio" id="Other" name="h12" value="Other">
<label for="Other">Other</label><br>
<input type="radio" id="Prefer not to say" name="h12" value="Prefer not to say">
<label for="Prefer not to say">Prefer not to say</label>
</div>
<div>
<p><strong>3. What is your ethnicity?</strong></p>
<input type="radio" id="White/Caucasian" name="h13" value="White/Caucasian">
<label for="White/Caucasian">White/Caucasian</label><br>
<input type="radio" id="Hispanic/Latino" name="h13" value="Hispanic/Latino">
<label for="Hispanic/Latino">Hispanic/Latino</label><br>
<input type="radio" id="Black/African American" name="h13" value="Black/African American">
<label for="Black/African American">Black/African American</label><br>
<input type="radio" id="Asian/Pacific Islander" name="h13" value="Asian/Pacific Islander">
<label for="Asian/Pacific Islander">Asian/Pacific Islander</label><br>
<input type="radio" id="Native American/American Indian" name="h13" value="Native American/American Indian">
<label for="Native American/American Indian">Native American/American Indian</label><br>
<input type="radio" id="Other" name="h13" value="Other">
<label for="Other">Other</label><br>
<input type="radio" id="Prefer not to say" name="h13" value="Prefer not to say">
<label for="Prefer not to say">Prefer not to say</label>
</div>
This is because your radio inputs have the same ID.
Every element needs to have unique ID, so just change some of them and you should be fine. Don't forget to update your for attributes aswell.
IDs are global in the DOM.
Your label is targeting the control with the id "Prefer not to say", so the first control with the ID that matches the for attribute in the label is activated.
Add a 1, 2, 3, etc... to the ids and for attri

For loop - bind dynamic key to the #id

I have a for loop that displays the data into multiple radio-buttons (bootstrap):
<div class="radio" >
<label v-for="(choice, index) in choices" v-if="choice.question_id == question.id" :key="choice.id">
<input type="radio" name="optradio" id="choice.id"> [[choice.content]]
</label>
</div>
As you can see, I wanted to use the choice.id for id="..." in each button, technically it should look something like this:
<input type="radio" name="optradio" id="1"> Choice1
<input type="radio" name="optradio" id="2"> Choice2
<input type="radio" name="optradio" id="3"> Choice3
But it renders it with the actual string choice.id:
<input type="radio" name="optradio" id="choice.id"> Choice1
<input type="radio" name="optradio" id="choice.id"> Choice2
<input type="radio" name="optradio" id="choice.id"> Choice3
Forgive my naiveness. Any help/advices? Thanks a lot!
It renders with choice-id string because you add plain string as the id value, not a variable value
You can use v-bind directive or the shorthand for v-bind -> :id
<div class="radio" >
<label v-for="(choice, index) in choices" v-if="choice.question_id == question.id" :key="choice.id">
<input type="radio" name="optradio" v-bind:id="choice.id"> [[choice.content]]
</label>
</div>
using shorthand <input type="radio" name="optradio" :id="choice.id">
To answer your questions in the comments.
You can ' separate 'the radios by adding them in a ' group ' using the name attribute. Radios with the same name attribute are in one group. Changing their values won't affect other radios in other groups ( with different name attributes ). See example below.
Or you can use vue v-model to separate and get the selected options.
new Vue({
el: "#radio-app",
data: {
question1: '',
question2: ''
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://github.com/vuejs/vue-devtools"></script>
Question1:
<input type="radio" name="question1" id="q1choice1" value="choice1"/>
<input type="radio" name="question1" id="q1choice2" value="choice2"/>
Question2:
<input type="radio" name="question2" id="q2choice1" value="choice1"/>
<input type="radio" name="question2" id="q2choice2" value="choice2"/>
<hr />
<h2> Or with VUE v-model </h2>
<div id="radio-app">
Question1:
<input type="radio" id="q1choice1vue" value="choice1" v-model="question1">
<input type="radio" id="q1choice2vue" value="choice2" v-model="question1">
Question2:
<input type="radio" id="q2choice1vue" value="choice1" v-model="question2">
<input type="radio" id="q2choice2vue" value="choice2" v-model="question2">
<div>Question 1 selected answer: {{ question1 }}</div>
<div>Question 2 selected answer: {{ question2 }}</div>
</div>
Check more here
VUE template syntax
v-bind directive
Use: v-bind:id="choice.id" or the short version :id="choice.id"

HTML Checkboxes in Form POST

vI am working with HTML and have a form with a group of checkboxes:
-> What are your favorite colors?
---> Red
---> Green
---> Blue
Say I selected, Red AND Green. In the form post, I want it to look like: "favorite-color": ["red", "green"]. Now, it only shows one of the colors, even if I select multiple. Here is some sample code:
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="red" name="color" value="red">
<label class="custom-control-label" for="red">Red</label>
</div>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="green" name="color" value="green">
<label class="custom-control-label" for="green">Green</label>
</div>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="blue" name="color" value="blue">
<label class="custom-control-label" for="blue">Blue</label>
</div>
My post, when I select red AND green:
{'color': 'green'}
By the way, I am using Django as my backend to parse the form POST.
Is there a way to do this? Thanks!
Via this blog:
For multiselects or inlines (one-to-many fields) that have multiple values, use .getlist():
So:
request.POST.getlist("color")
Give all the input type="checkbox" elements the same value for their name attribute and then the values from the checked checkboxes will be submitted as a single value for the one name. In most server-side environments, that means that if you request that form value, you'll get all the values with the single form field request.

Distinguishing checkboxes with the same ID in different classes

Hope you can help me regarding webscraping via VBA. In a webform I have to check and uncheck checkboxes using VBA. Now I run into the difficulty that some checkboxes have the same ID but reside in different classes.
Example:
Class "filter" has a checkbox with ID "start_date" and class "return_fields" has a checkbox with ID "start_date". When using .getElementByID("start_date") both checkboxes switch simultaneously.
But in this case I want to have the checkbox in class "filter" to be checked and the one in "return_fields" to be unchecked.
So I've attempted something like
.getElementsByClassName("filter")(0).getElementById("start_date").checked = True
.getElementsByClassName("return_fields")(0).getElementById("start_date").checked = False
Unfortunately this returns an error "Object doesn't support this property or method".
Here is the content of the element I get with .getElementByClassName("filter"):
<legend>Date Mode</legend>
<div class="start_date required">
<label for="active_date">Start Date</label>
<input name="date_mode" class="radio" id="start_date" type="radio" checked="checked" value="start_date">
</div>
<div class="report_date required">
<label for="report_date">Report Date</label>
<input name="date_mode" class="radio" id="report_date" type="radio" value="report_date">
</div>
<div class="end_date required">
<label for="end_date">End Date</label>
<input name="date_mode" class="radio" id="end_date" type="radio" value="end_date">
</div>

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".