I have two radio buttons:
<p>
<label for="input_1">Path to Excellent </label>
<input id="input_1" name="radio" type="radio" value="1" />
</p>
<p>
<label for="input_2">Award of Excellence</label>
<input id="input_2" name="radio" type="radio" value="2" />
</p>
I would like to get val() from one of them using their ids: "input_1" or "input_2" by jQuery, something like this:
$('input[#input_1,#input_2]:checked').val();
My aim is to get val() from one of them that is checked, by id. How can I do this?
You can check the starts of the id in this case:
$("input[id^='input_']:checked").val();
Related
I am very new to javascript and I can't set the text next to the Radio button to what I want, even after trying to find everything I can online. I am sure it something simple and I would really apprecitae it if someone could help me.
Here is my HTML Element for my radio group
<form class="description" action ="">
<input type="radio" id="answer0id" name="answers" value="answer0"> answer0<br>
<input type="radio" id="answer1id" name="answers" value="answer1"> answer1<br>
<input type="radio" id="answer2id" name="answers" value="answer2"> answer2<br>
<input type="radio" id="answer3id" name="answers" value="answer3"> answer3<br>
</form>
And this is the Javascript I am trying to run to test just changing one
document.getElementById('answer0id').value = 'testing123';
I know it is probably an easy fix, but I would really appreciate anyone who could help me.
The text is just a rogue text node, as the input is self-closing and can't contain any inner text, but you can target the text node coming after the input with something like nextSibling
document.getElementById('answer0id').nextSibling.textContent = ' testing123';
<form class="description" action ="">
<input type="radio" id="answer0id" name="answers" value="answer0"> answer0<br>
<input type="radio" id="answer1id" name="answers" value="answer1"> answer1<br>
<input type="radio" id="answer2id" name="answers" value="answer2"> answer2<br>
<input type="radio" id="answer3id" name="answers" value="answer3"> answer3<br>
</form>
You could also modify the HTML to wrap the text in either a span, label or some other element that is more easily selectable.
document.querySelector('#answer0id ~ label').innerHTML = 'testing123';
<form class="description" action ="">
<input type="radio" id="answer0id" name="answers" value="answer0" />
<label for="answer0id">answer0</label>
<br />
<input type="radio" id="answer1id" name="answers" value="answer1" />
<label for="answer1id">answer1</label>
<br />
<input type="radio" id="answer2id" name="answers" value="answer2" />
<label for="answer2id">answer2</label>
<br />
<input type="radio" id="answer3id" name="answers" value="answer3" />
<label for="answer3id">answer3</label>
</form>
I believe that label is what you are looking for.
Note: Input doesn't have innerHTML/textContent attributes since it's a non-closing tag. That's why the inventor of HTML came up with labels. What's more - you are even able to check the radio button by clicking the corresponding text.
let elem = document.querySelector('label[for="answer0id"]');
elem.textContent = 'testing123';
<form class="description" action="">
<input type="radio" id="answer0id" name="answers" value="answer0">
<label for='answer0id'>answer0</label><br />
<input type="radio" id="answer1id" name="answers" value="answer1">
<label for='answer1id'>answer1</label><br />
<input type="radio" id="answer2id" name="answers" value="answer2">
<label for='answer2id'>answer2</label><br />
<input type="radio" id="answer3id" name="answers" value="answer3">
<label for='answer3id'>answer3</label>
</form>
document.getElementById('answer0id').value= 'testing123'; refers to <input type="radio" id="answer0id" name="answers"value="answer0"> answer0<br>. Therefore, it does not change the text.
In your case, you can select the text via .nextSibling.textContent (see below). An alternative would be wrapping the text in an extra element, say <span id="answer0-label">answer0</span> and select that one directly.
document.getElementById('answer0id').nextSibling.textContent = ' testing123';
<form class="description" action ="">
<input type="radio" id="answer0id" name="answers" value="answer0"> answer0<br>
<input type="radio" id="answer1id" name="answers" value="answer1"> answer1<br>
<input type="radio" id="answer2id" name="answers" value="answer2"> answer2<br>
<input type="radio" id="answer3id" name="answers" value="answer3"> answer3<br>
</form>
if you have this code:
<form name="thing">
<label>Are you a student?</label>
<input name="choice" type="radio" value = "yes" checked />yes
<input name="choice" type="radio" value="no" />no
</form>
then you can select the radios like so:
document.thing.choice[i].value
And then I assumed that in this case:
<form name="thing">
<label name ="yes_no">Are you a student?
<input name="choice" type="radio" value = "yes" checked />yes
<input name="choice" type="radio" value="no" />no
</label>
</form>
then you could select the radio values like so:
document.thing.yes_no.choice[i].value
why does this work for form and not label? Is it because form is an object in the DOM and label is not?
Also, is the name attribute valid for form still? Or should one only use ID's...
i am working on html and CSS. i have to add 5 radio buttons to my page and i have added within <label> tag. but when i look for the page. it shows all the radio buttons selected and also i am unable to unselect it. the thing is i need only one radio button selected at a time. here is my code.
<label class="radio"><input type="radio"> Pepse</label>
<label class="radio"><input type="radio"> Coke</label>
<label class="radio"><input type="radio">Mirinda</label>
<label class="radio"><input type="radio">Maaza </label>
radio buttons require a common name. If you don't give them a name attribute, each radio button essentially becomes a one-way checkbox. You can select them, but you can't UNselect them.
<input type="radio" name="foo" value="1" />
<input type="radio" name="foo" value="2" />
<input type="radio" value="3" />
In this case, the two foo radio buttons will be linked internally because they are both named the same, but the one with value 3 will be completely independent and act as your are.
Add a group name.
jsFiddle
<label class="radio"><input name="drinks" type="radio">Pepse</label>
<label class="radio"><input name="drinks" type="radio">Coke</label>
<label class="radio"><input name="drinks" type="radio">Mirinda</label>
<label class="radio"><input name="drinks" type="radio">Maaza </label>
<label class="radio"><input name="drinks" type="radio">Milk Frothers</label>
1.agroup of radios need a name so that the browser know which one is selected
2.if u want to put the label outside of the input u can use the for attribute
to tell the browser that this label is for that radio with the same id
<label for="a">a</label>
<input type="radio" name="aname" id="a" value="a"><br>
<label for="b">b</label>
<input type="radio" name="aname" id="b" value="b"><br>
<label for="c">c</label>
<input type="radio" name="aname" id="c" value="c"><br>
<label for="d">d</label>
<input type="radio" name="aname" id="d" value="d"><br>
but i also prefer radios inside labels so
<label><input type="radio" name="aname" value="a">a</label><br>
<label><input type="radio" name="aname" value="b">b</label><br>
<label><input type="radio" name="aname" value="c">c</label><br>
<label><input type="radio" name="aname" value="d">d</label><br>
<label><input type="radio" name="aname" value="e">e</label><br>
3.in a common way they also need a value, except ur using js
<label><input type="radio" name="aname">a</label><br>
<script>
document.write(document.getElementsByTagName('label')[0].childNodes[1].nodeValue)
</script>
writes a after <br>
I've got multiple sets of radio buttons and am trying to use the .find() function to dynamically find the value of radio buttons in the same grouping.
However, it keeps returning undefined.
<fieldset>
<div id="border1">
<input id="radio1a" type="radio" id="set1" class="animals radio" value="Zebra">
<input id="radio1b" type="radio" id="set1" class="animals radio" value="Lion">
</div>
<div id="border2">
<input id="radio2a" type="radio" id="set2" class="fruit" value="Oranges">
<input id="radio2b" type="radio" id="set2" class="fruit" value="Grapes">
</div>
</fieldset>
<fieldset>
<div class="border1">
<input id="radio3a" type="radio" id="set3" class="animals radio" value="Monkey">
<input id="radio3b" type="radio" id="set3" class="animals radio" value="Parrot">
</div>
<div class="border2">
<input id="radio4a" type="radio" id="set4" class="fruit radio" value="Bananas">
<input id="radio4b" type="radio" id="set4" class="fruit radio" value="Cherries">
</div>
</fieldset>
(Sorry, didn't mean to put the same IDs. Was a copy/paste.)
I'm trying to use jquery to dynamically find the values:
$(".animals .radio").change(function()
{
alert($(this).closest('fieldset').find('.fruit').val());
etc.
}
But it keeps returning undefined
Also tried:
$(this).closest('fieldset').find('.fruit:checked').val()
Is there another way I should be approaching this?
I don't want to have to write code for every single set of radio buttons.
$(".animals .radio") is not the query you are looking for, it should be $(".animals.radio") (no white space between classes).
$(".animals .radio") looks for an element with class "radio" inside an element with class "animals".
It should .animals.radio, not .animals .radio.
.animals.radio means these two classes belongs to same element. (in your case this is right)
.animals .radio means .animals is ancestor of .radio.
$(".animals.radio").change(function() {
alert($(this).closest('fieldset').find('.fruit').val());
});
AND ONE THINK, YOUR CODE HAVE DUPLICATE IDS, AVOID IT.
How about not using id="" twice, and setting a type="radio" on those inputs for starters ?
You are also using the same ID for multiple elements, stop that ?
It should probably be :
<fieldset>
<div id="border1">
<input type="radio" id="radio1a" name="set1" class="animals radio" value="Zebra" />
<input type="radio" id="radio1b" name="set1" class="animals radio" value="Lion" />
</div>
<div id="border2">
<input type="radio" id="radio1c" name="set2" class="fruit" value="Oranges" />
<input type="radio" id="radio1d" name="set2" class="fruit" value="Grapes" />
</div>
</fieldset>
<fieldset>
<div class="border1">
<input type="radio" id="radio2a" name="set3" class="animals radio" value="Monkey" />
<input type="radio" id="radio2b" name="set3" class="animals radio" value="Parrot" />
</div>
<div class="border2">
<input type="radio" id="radio2c" name="set4" class="fruit radio" value="Bananas" />
<input type="radio" id="radio2d" name="set4" class="fruit radio" value="Cherries" />
</div>
</fieldset>
To get both values :
$('input[type="radio"]').on('change', function() {
$(this).closest('fieldset').find('.fruit').each(function() {
alert(this.value);
});
});
To get only the one that is checked :
$('input[type="radio"]').on('change', function() {
var elm = $(this).closest('fieldset').find('.fruit').filter(':checked');
alert(elm.value);
});
$(".animals .radio") is getting you all the elements with class radio that have parents with class animals.
You want elements with both classes animals and radio, which is like $(".animals.radio")
I have a group of radio buttons on my page:
<form ...>
<input type="radio" name="people" checked> Student
<input type="radio" name="people"> Teacher
<input type="radio" name="people"> Assistant
<!-- Here is the dynamic content, which could be check boxes or radio buttons-->
</form>
The feature I would like to implement is:
Based on the selection of the radio buttons, the content after the radio buttons will change dynamically. (The radio buttons and the content are inside a form.)
For example:
If "student" is selected, the dynamic content part is (check boxes):
<input type="checkbox" name="name" /> Name <br />
<input type="checkbox" name="Age" /> Age <br />
<input type="checkbox" name="grade" /> Grade <br />
If "Teacher" is selected, the dynamic content part is (check boxes & radio buttons):
<input type="checkbox" name="subject" /> Subject <br />
<input type="radio" name="code" checked> 111
<input type="radio" name="code"> 222
<input type="radio" name="code"> 333
If "Assistant" is selected, the dynamic content part is other check boxes.
How to implement this dynamic content change in jQuery?
What I tried
I tried to create HTML elements dynamically in Javascript, but I feel it is not a good way since I have to write HTML elements in Javascript as strings.
Try this
Working demo
Markup change
<form ...>
<input type="radio" name="people" value="student" checked> Student
<input type="radio" name="people" value="teacher"> Teacher
<input type="radio" name="people" value="assistant"> Assistant
<div class="content student">
<input type="checkbox" name="name" /> Name <br />
<input type="checkbox" name="Age" /> Age <br />
<input type="checkbox" name="grade" /> Grade <br />
</div>
<div class="content teacher" style="display:none;">
Teacher content
</div>
<div class="content assistant" style="display:none;">
Assistant content
</div>
</form>
Js
$(function(){
$("input[name=people]").click(function(){
$("div.content").not("."+this.value).hide();
$("."+this.value).show();
});
});
put all three possible elements in your static html and wrap each part with a div. Then Show and hide the divs on click
If I understand your question correctly... I would create different divs that are hidden that contain the combinations that you are looking for. Then on the onclick of the radio button I would hide divs that you don't want shown and show the divs that you are looking for.