Jquery - remove radio button - javascript

I have two radio buttons with text label. I try to remove the radio button that does not have value then the radio button was remove successfully, but the problem is its text label, I also want to remove them but i can not. Can anyone show me how to do that? Thank you so much for reading my question!

Add a class to the label when you remove the button with the name hidden for example. Then in the .css set the .hidden { display: none;}

you have two options
you can add same class name for both radio and text field and hide the class
eg:
<input type="radio" class="myclass" >
<label class="myclass">abc</label>
$(".myclass").hide();
2.also you can add input feild and lable to Div,hide div
eg:-
<div id="mydiv">
<input type="radio" ><label >abc</label>
</div>
$("#mydiv").hide();

This will work for you, use element.each to find all the radios without value and remove them ...
Here in this code, only first radio will be displayed because it has value and the second one is removed and the label for second is also removed...
$(document).ready(function(){
$('input[type="radio"]:not([value])').each(function() {
var idVal = $(this).attr("id");
$(this).remove();
$("label[for='"+idVal+"']").remove();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name="submit" id="submit" action="#" method="post">
<label for="one">First Item</label>
<input type="radio" id="one" name="first_item" value="1" />
<label for="two">Second Item</label>
<input type="radio" id="two" name="first_item" />
</form>

Related

Using jquery to enable div on a page based on a radio button selection

I want to be able to enable a div if yes or no was selected from a radio button using jquery. The default is no div shows up
when the page loads. When you now select a radio button it determines which div shows up.
Here is what the html code will look like
<html>
<head>Testing</head>
<body>
<p>Are you 70 years and older? <input type="radio" value="yes" name="myradiobutton" /> Yes <input type="radio" value="no" name="myradiobutton" /> No</p>
<div style="display: none" id="yes">
<p>Yes was selected</p>
</div>
<div style="display: none" id="no">
<p>No was selected</p>
</div>
</body>
</html>
It is a long time I did jquery but was wondering how I can turn this on and off with jquery below.
How can I write this such that I will be able to control the styles on each div via jquery(turning it on and off based on the radio button selection)?
$(".myradiobutton").click(function(){
var selectedval = $("#myradiobutton input[type='radio']:checked").val();
if(selectedval == "yes"){
//show the yes div
}
if(selectedval == "no"){
//show the no div
}
});
Id must be unique .. You've to use it for only one element .. You can use same class for divs so you can hide both of them in one selection .. Also you can use the code like this instead of going with if statement .. And I prefer to use change event instead of click for radio,checkbox,select
$("[name='myradiobutton']").on('change' , function(){
var selectedval = $("input[type='radio'][name='myradiobutton']:checked").val();
$('.just_test').hide(0).filter('#'+selectedval).show();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>Are you 70 years and older? <input type="radio" value="yes" name="myradiobutton" /> Yes <input type="radio" value="no" name="myradiobutton" /> No</p>
<div style="display: none" class="just_test" id="yes">
<p>Yes was selected</p>
</div>
<div style="display: none" class="just_test" id="no">
<p>No was selected</p>
</div>
Don't forget to add class="just_test" to the divs in html
You tried to use #myradiobutton as a query selector but the # represents the id attribute of an element which you don't have in your HTML code.
To fix it, you can't have your two <input type="radio"> elements both using the myradiobutton as id.
Instead of that, my suggestion is to use <label> to represent the <input>s.
Example below:
<input type="radio" name="age" value="yes" id="older-than-70">
<label for="older-than-70">Yes</label>
<input type="radio" name="age" value="no" id="younger-than-70">
<label for="younger-than-70">No</label>
<input> element uses attribute id to identify itself and uses attribute name="age" to group with other <input>s.
<label> element uses attribute for as a pointer to hook to the <input> with matching id.

how to save radio button clicked value in hidden field?

<script>
$(document).ready(function(){
var value = $('input[name="yesno"]').change(function(){
if($('#imageCheck').prop('checked')){
// alert('Image Option checked!');
}else if($('#textCheck').prop('checked')){
// alert('Text Option Checked!');
}
});
});
</script>
<input type="radio" onclick="javascript:imageTextCheck();" name="yesno"
id="imageCheck"/>&nbsp&nbsp&nbsp<b>Text</b>
<input type="radio" onclick="javascript:imageTextCheck();"name="yesno"
id="textCheck"/>
<input type="hidden" name="radioCheck" id="radioCheck" value=""/>
how i can save the value of radio button in hidden field. If i Click on image it saves image value.
$(document).ready(function(){
var value = $('input[name="yesno"]').click(function(){
$('#radioCheck').val($(this).val());
});
setInterval(() => console.log( $('#radioCheck').val() ), 1000);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" name="yesno" value="imageCheck" id="imageCheck"/><label for="imageCheck">imageCheck</label>
<br />
<input type="radio" name="yesno"value="textCheck" id="textCheck"/><label for="textCheck">textCheck</label>
<input type="hidden" name="radioCheck" id="radioCheck" value=""/>
hope this one helps you :)
the setInterval is just to print out the hidden field's value each and every second so when you click on a radio button you can see that it sets the hidden input's value
Here is a simple solution for your problem: I have written one sample of code to explain the problem:
First: OnClick of radio button save the value of radio button if it is checked.
var radioValue = $("input[name='yesno']:checked").val();
Second: Assign the checked radio button value to the hidden input field.
$(document).ready(function(e) {
$("input[type='radio']").click(function(){
var radioValue = $("input[name='yesno']:checked").val();
if(radioValue) {
$("#radioCheck").val(radioValue);
console.clear();
console.log($("#radioCheck").val());
}
});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="image_radioButton">
<input id="image_radio_check" value="imageButtonChecked" type="radio" class="radio_button" name="yesno" /> Image Radio Button
</div>
<div class="text_radioButton">
<input id="text_radio_check" value="textButtonChecked" type="radio" class="radio_button" name="yesno" />Text Radio Button
</div>
<div class="hidden_input">
<input type="hidden" name="radioCheck" id="radioCheck" value=""/>
</div>
Hope this may hep you in solving your problem.

Make check-boxes becoming one group of radio button

I cannot make the input name same or value same. The second and third inputs come from a loop using c# razor. I have 2 sets of radio inputs first one is one set and second and third are another set. Because the second and third have the same name, checking one makes the other unchecked. I want the same for all of them together so it would be like I have one set of 3 radio buttons. Like I said above I am not able to make the name or value same due to back-end data display issue. Here is my attempt below.
//first radio <br/>
<div class="radio">
<label>
<input id="dcenter-allradio" type="radio" value="0" />All
</label>
</div>
//this radio button is a loop <br>
<input type="radio" name="#Model.Facet.Key" value="#item.Key">tagitem.j
<div class="radio">
<label>
<input id="dcenter-listradio" type="radio" name="#Model.Facet.Key" value="#item.Key" />tagItem.Name
</label>
</div>
<script>
$(document).ready(function () {
if ($('#dcenter-listradio').prop("checked", true)) {
$('#dcenter-allradio').prop("checked", false);
}
if ($('#dcenter-allradio').prop("checked", true)) {
$('#dcenter-listradio').prop("checked", false);
}
});
</script>
If you can give them all the same class, then you can just use jQuery to detect when a change has occurred and then uncheck other items in the same class.
$(document).ready(function() {
var selector = ".groupTogether";
// or if you can't give same class, you could use "#unrelatedRadio, input[name='related']"
$(selector).change(function()
{
if(this.checked)
{
$(selector).not(this).prop('checked', false);
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input id="unrelatedRadio" name="unrelated" type="radio" class="groupTogether">unrelated</input>
<input id="relatedA" name="related" type="radio" class="groupTogether">Related A</input>
<input id="relatedB" name="related" type="radio" class="groupTogether">Related B</input>
Or, if you can't give them the same class, just replace the selector with something that selects both sets (in my example, "#unrelatedRadio, input[name='related']")
let radios = document.querySelectorAll("input");
for (let i of radios){
i.name="same"
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
//first radio <br/>
<div class="radio">
<label>
<input id="dcenter-allradio" type="radio" value="0" />All
</label>
</div>
//this radio button is a loop <br>
<input type="radio" name="#Model.Facet.Key" value="#item.Key">tagitem.j
<div class="radio">
<label>
<input id="dcenter-listradio" type="radio" name="#Model.Facet.Key" value="#item.Key" />tagItem.Name
</label>
</div>

Confusion over radio buttons

I'm iterating over radio buttons previous neighbors. My html looks like this :
<div class="holder">
<span class="">some text</span>
<input type="radio" name="test_capability" value="primary" checked="" />
</div>
<div class="holder">
<span class="">some text</span>
<input type="radio" name="test_capability" value="primary" checked="checked" />
</div>
<div class="holder">
<span class="">some text</span>
<input type="radio" name="test_capability" value="primary" checked="" />
</div>
I'm trying to add class red to span whose neighbor radio button is checked. Here is how I tried :
$('.holder span').each(function(){
var spanElement = $(this);
if(spanElement.nextAll(':radio:first').is(':checked')){
spanElement.addClass("red");
}
});
I always end up with last span (the one in third holder div) having class red. But as you can see the middle one is checked. Same thing happens when I set checked=checked on first span
Update
Tried also :
$('.holder span').each(function(){
if($(this).next().attr("checked") == "checked"){
$(this).addClass("red");
}
});
Same result, don't know what else to try.
Update II:
Html get rendered with previous information generated from server side, so there can only one checked=checked radio input element once html is created.
I'm doing this iteration trough span in document ready function.
You can select the checked input and add class to it's sibling span element.
$('.holder input[type=radio]:checked').prev('span').addClass('red')
http://jsfiddle.net/TdR7k/
In case that you want to add and remove the classes on change event you can try the following:
$('.holder input[type=radio]').change(function() {
$('.red').removeClass('red');
$(this).prev('span').addClass('red')
}).change()
http://jsfiddle.net/dSv6P/
Try this change:
$('.holder span').each(function(){
var spanElement = $(this);
if(spanElement.next('input[type=radio]').is(':checked')){
spanElement.addClass("red");
}
});
The problem is the way you define the radio buttons, do not include the checked attribute in the html or they will be checked by default
example: http://jsfiddle.net/TdR7k/1/
<div class="holder">
<span class="">some text</span>
<input type="radio" name="test_capabiy" value="primary" checked="" />
</div>
<div class="holder">
<span class="">some text</span>
<input type="radio" name="test_capability" value="primary" checked="checked" />
</div>
<div class="holder">
<span class="">some text</span>
<input type="radio" name="test_capabiliy" value="primary" checked />
</div>​
All three are checked
The proper definition of a radio button is
<input type="radio" name="(Group)" value="(etc)" />
And to check it by default, all that is required:
<input type="radio" name="(Group)" value="(etc)" checked />
You were adding three radio buttons of the same group that were all checked by default, so the last one was overriding the previous two and getting the red class
But mainly for the actual answer, do not include checked as an attribute and apply the javascript as an onclicked event, or only add the checked to a single element
here is an example of an onclick event:
http://jsfiddle.net/7kbLf/2/
If the Radio Button contains the checked attribute . Then it is by default checked..
<input type="radio" name="group1" value="hello" checked> // Checked by default.
So remove the checked property for other two RadioButtons.. if you don't do so because of the syntax the last item with checked property is checked by default..
<div class="holder">
<span>some text</span>
<input type="radio" name="test_capability" value="primary" />
</div>
<div class="holder">
<span>some text</span>
<input type="radio" name="test_capability" value="primary" checked="checked"/>
</div>
<div class="holder">
<span>some text</span>
<input type="radio" name="test_capability" value="primary" />
</div>
If you set the HTML this way your approach should work..
$('.holder span').each(function() {
var spanElement = $(this);
if (spanElement.nextAll(':radio:first').is(':checked')) {
spanElement.addClass("red");
}
});
// Change event
$('input[type="radio"]').on('change', function() {
$('span').removeClass('red');
if( this.checked){
$(this).closest('div.holder').find('span').addClass('red');
}
});
CHECK FIDDLE

jQuery: how to print out the text label of radio buttons?

If the code is
<form>
<input type="radio" name="font"> Arial</input><br>
<input type="radio" name="font"> Times New Roman</input><br>
<input type="radio" name="font"> Monaco</input><br>
</form>
<script>
$('form input').each(function(i, e) {
alert($(this).text())
})
</script>
It shows 3 empty strings. How can it show the 3 names of fonts?
try it at: http://jsfiddle.net/bKhsp/3/
You can't have text inside an <input> tag - it's invalid HTML even though the browser will try to render it, if you want that you should use a <label> wrapper (which keeps the text clickable as well) like this:
<form>
<label><input type="radio" name="font" value="Arial"> Arial</label><br>
<label><input type="radio" name="font" value="Times New Roman"> Times New Roman</label><br>
<label><input type="radio" name="font" value="Monaco"> Monaco</label><br>
</form>
With a loop to match:
$('form input').each(function(i, e) {
alert($(this).parent().text()); //for the label text
alert($(this).val()); //for the input value
});
You can view the updated/working fiddle here. For postback reasons, you probably want a value on the inputs as well, so a value for font gets sent.
Inputs never have end tags, meaning to be correct, you'd have to have the following:
<form>
<input type="radio" name="font" /> Arial<br />
<input type="radio" name="font" /> Times New Roman<br />
<input type="radio" name="font" /> Monaco<br />
</form>
From there, you can take measures to tie the text with its respective radio button:
<form>
<input type="radio" name="font" id="fontArial"/><label for="fontArial">Arial</label><br />
<input type="radio" name="font" id="fontTimes"/><label for="fontTimes">Times New Roman</label><br />
<input type="radio" name="font" id="fontMonaco"/><label for="fontMonaco">Monaco</label><br />
</form>
<script type="text/javascript">
$('form label').each(function(i, e) {
alert($(this).text());
})
</script>
For grabbing the respective radio button from the label, simply take the for attribute for a selector id (for example this takes the value of a label's respective radio button):
$('#' + $(this).attr('for')).val();
The input element doesn't have a closing tag. Use a label around the radio button and text, then the text is clickable as it should be:
<form>
<label><input type="radio" name="font"/><span>Arial</span></label><br>
<label><input type="radio" name="font"/><span>Times New Roman</span></label><br>
<label><input type="radio" name="font"/><span>Monaco</span></label><br>
</form>
Putting the text in a span is a good idea, then you can target it separately so that you can style it with CSS.
Use the next function to reach the text from the input:
$('form input').each(function(i, e) {
alert($(this).next().text())
});
You should use a <label> element and specify the id of the input element in its for attribute, that way when you click on the text it will check the radio input for you. As Nick Craver mentioned, you can't have text nodes inside an input element.
You can use nextSibling to get the text node following an element, then access its nodeValue property for the text:
$('form input').each(function(i, e) {
alert(this.nextSibling.nodeValue);
});
This should also be more efficient than solutions that wrap this with jQuery. You can also use this.value to get the value without wrapping and calling .val().
Updated fiddle at http://jsfiddle.net/AndyE/bKhsp/6/.
The HTML markup is incorrect. Each option needs a value:
<form>
<input type="radio" name="font" value="Arial"> Arial <br>
<input type="radio" name="font" value="Times New Roman"> Times New Roman<br>
<input type="radio" name="font" value="Monaco"> Monaco<br>
</form>
Your jQuery can then look something like this (from memory):
$('form input').each(function(i, e) {
alert($(this).val());
})

Categories