I'm using bootstrap and I find it confusing when trying to extract the label name. How do I get the selected label text? (e.g. Furniture or Fruits)
<div class="input-group">
<span class="input-group-addon">
<input type="radio" name="radioGroup" value="myval1" /></span>
<input class="form-control" value="Furniture" disabled>
</div>
<div class="input-group">
<span class="input-group-addon">
<input type="radio" name="radioGroup" value="myval2" /></span>
<input class="form-control" value="Fruits" disabled>
</div>
I tried these but did not give the label text:
alert($( $(":radio[name=radioGroup]:checked").prop("labels") ).text());
alert($('input[name="radioGroup"]:checked').text());
alert($('input[name="radioGroup"]:checked + label').text());
In your scenario you just need to get the value of selected radio button.
For your better understanding have a lookto this JS Fiddle link
You can use the selected DOM element as::
alert($("input[name='radioGroup']:checked").closest(".input-group").find(".form-control").val())
Try using .val() then follow the tree from the checkbox up to its containing div and back down to the value in the associated input.
alert($("input[name=radioGroup]:checked").parent(".input-group-addon").parent(".input-group").children("input.form-control").val());
You can try this...
$('input[name="radioGroup"]:checked').parents('.input-group').find('.form-control').val()
OR
$('input[name="radioGroup"]:checked').parent().next().val()
Related
I have this HTML code
<div class="option-wrapper">
<label class="">
<input type="checkbox" name="name" value="1" data-type-xml="select" aria-label="Untitled">
<span lang="" class="option-label active">1</span>
</label>
<label class="">
<input type="checkbox" name="name" value="2" data-type-xml="select" aria-label="Untitled">
<span lang="" class="option-label active">2</span>
</label>
</div>
im trying to find the labels and to set a "style="display: none;"
like this <label class="" style="display: none;>
my problem is, I only need one of the labels in this div. But I don't have a class name or id to find one label. Do u guys know a solution for this?
Use document.getElementsByTagName('label') instead for all end then get them with the array offset. Or - to select the first - use document.getElementByTagName('label'). Notice the singular version of the second selector.
Or, a more general selector which you can use with either classes or tags or any other query string would be document.querySelector('label') or document.querySelectorAll('label')
I'm trying to target the content and making the entire input fields hidden or disable specifically the field "Pickup in Store : $0.00".
I can only get the radio button to be hidden but not the text beside the radio button. I know the Labels are global containers but I can see the input fields has a unique ID. Is there a way we can accomplish this e.g. CSS, Javascript or Jquery?
<div class="shopping-basket-shipping-methods" data-shopping-basket-id="35838">
<label class="user-form-field-label">
<input class="shopping-basket-shipping-method" data-cart-id="411539" data-shopping-basket-id="35838" id="shopping_baskets_attributes_35838_shipping_method_id_932" name="shopping_baskets_attributes[35838][shipping_method_id]" type="radio" value="932">
Canada Post - Ground : $20.00
</label><br>
<label class="user-form-field-label">
<input checked="checked" class="shopping-basket-shipping-method" data-cart-id="411539" data-shopping-basket-id="35838" id="shopping_baskets_attributes_35838_shipping_method_id_915" name="shopping_baskets_attributes[35838][shipping_method_id]" type="radio" value="915">
<p>Pickup in Store : $0.00</p>
</label><br>
</div>
Personally I see three options;
Wrapping both elements in a single div and targeting them together (most straight foward)
Targeting the previous sibling as well (not difficult with css)
Assigning the label an equally descriptive and dynamic id for easier targeting (might require a bit of extra work)
You shouldn't use the label element to wrap the radio and the text.
<div class="form-control">
<label for="option1">Option 1 description</label>
<input type="radio" name="shopping" id="option1" value="option1">
</div>
<div class="form-control">
<label for="option2">Option 2 description</label>
<input type="radio" name="shopping" id="option2" value="option2">
</div>
If you want to relate some text to an input field you can simply add a label element as a sibling of the input element and relate them with the for attribute matching the input's id that's going the make the html structure more solid, you can also use an extra class or add an id for the form-controls div to make it easier to select or if you are already using jQuery you can target these elements parent with something like $('#option1').parent()
No need to hide all the label simply hide its content considering the ID of the input:
#shopping_baskets_attributes_35838_shipping_method_id_915,
#shopping_baskets_attributes_35838_shipping_method_id_915 + *{
display:none;
}
<div class="shopping-basket-shipping-methods" data-shopping-basket-id="35838">
<label class="user-form-field-label">
<input class="shopping-basket-shipping-method" data-cart-id="411539" data-shopping-basket-id="35838" id="shopping_baskets_attributes_35838_shipping_method_id_932" name="shopping_baskets_attributes[35838][shipping_method_id]" type="radio" value="932">
Canada Post - Ground : $20.00
</label><br>
<label class="user-form-field-label">
<input checked="checked" class="shopping-basket-shipping-method" data-cart-id="411539" data-shopping-basket-id="35838" id="shopping_baskets_attributes_35838_shipping_method_id_915" name="shopping_baskets_attributes[35838][shipping_method_id]" type="radio" value="915">
<p>Pickup in Store : $0.00</p>
</label><br>
</div>
I am currently trying to make an entire Bootstrap input-group to be able to clickable so that the checkbox will be clicked when any part of the input-group is clicked, and it should be able to uncheck the checkbox as well when the input-group is clicked. The current code I have working right now is this:
$(document).ready(function(){
$('.input-group,#opt_in').click(function(){
if ($('#opt_in').is(':checked')) {
$('#opt_in').prop('checked', false);
}
else{
$('#opt_in').prop('checked', true);
}
});
});
<div class="input-group mb-3">
<div class="input-group-prepend">
<div class="input-group-text">
<input type="checkbox" name="opt_in" id="opt_in" value="true">
<label class="form-check-label">I CONFIRM</label>
</div>
</div>
<p class="form-control" aria-label="Text input with checkbox" id="consent">that all of my info is accurate and consent to be called as provided above</p>
</div>
But for some reason that I am unaware of, it's not working. Any reason why? I'm using jQuery here but I can also use vanilla javascript. I'm just not sure where my code is freaking out!
I am trying to set up a set of radio buttons using Bootstrap in javascript
The code I am trying is:
var viewedFilterButtons = $("<div>").addClass("btn-group").attr("data-toggle", "buttons");
viewedFilterButtons.append($("<label>").addClass("btn").addClass("btn-primary").append($("<input>").attr("id", "viewed-important").attr("type","radio").attr("name","viewed-filter").attr("value","important").attr("autocomplete","off").append($("<label for=\"viewed-important\">").text("Important"))));
viewedFilterButtons.append($("<label>").addClass("btn").addClass("btn-primary").append($("<input>").attr("id", "viewed").attr("type","radio").attr("name","viewed-filter").attr("value","viewed").attr("autocomplete","off").text("Reviewed")));
(note that I'm trying 2 different things to get the text into the button -- in the first input I'm embedding a label and in the second I'm just trying to set the text of the input. Neither is working right now.)
This generates the following HTML:
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-primary active">
<input id="viewed-important" name="viewed-filter" value="important" autocomplete="off" type="radio">
<label for="viewed-important">Important</label>
</input>
</label>
<label class="btn btn-primary">
<input id="viewed" name="viewed-filter" value="viewed" autocomplete="off" type="radio">Reviewed
</label>
</div>
Note that the input doesn't seem to be closed in the second case. I'm getting this HTML from the Web Console Inspector in Firefox.
What I'm getting is a set of tiny radio buttons with no text. The toggle behavior works fine.
What am I missing here? When I manually generate a set of labels for radio buttons like this directly in the HTML it works fine.
<label class="btn btn-primary">
<input id="viewed" name="viewed-filter" value="viewed" autocomplete="off" type="radio">Reviewed</input>
</label>
This is because you append the <label> inside the <input/>.
You're trying to do this :
<input> <label>Important</label> </input>
However, <input/> is a self-closing tag, not a container like a <div>.
You should design your structure like this :
<label> <input/> Important </label>
input elements cannot have children w3 specification. Put the label after it.
var viewedFilterButtons = $("#mainDiv").addClass("btn-group").attr("data-toggle", "buttons");
var inputElem = $("<input>").attr("id", "viewed-important").attr("type","radio").attr("name","viewed-filter").attr("value","important").attr("autocomplete","off");
viewedFilterButtons.append(inputElem)
.append($("<label for=\"viewed-important\">").addClass("btn").addClass("btn-primary").text("Important"));
<link href="https://github.com/twbs/bootstrap/blob/v4-dev/dist/css/bootstrap.css" rel="stylesheet"/>
<div id="mainDiv"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
I have a gender selection radio:
<div class="label-inputs" name="userFieldDiv" id="genderUserFieldDiv">
<label class="required">Sexo</label>
<input type="radio" class="check" value="F" name="userDto.gender" id="userDto.gender0">
<label for="userDto.gender0">Femenino</label>
<input type="radio" class="check" checked="checked" value="M" name="userDto.gender" id="userDto.gender1">
<label for="userDto.gender1">Masculino</label>
</div>
I'm trying to use a jQuery script to get the selected radio and paste it inside of a label.
$("#userDto\\.gender").change(function() { $("#genderLabel").html(this.value); });
The problem is that I'm using Spring, and when I use formRadioButton, it generates the id: userDto.gender but adds a 0 and 1 to the options. So I'm out of ideas about how to make the next HTML to get the value of the selected radio.
<div name="userLabelDiv" id="genderUserLabelDiv">
<label class="required">Sexo</label>
<label id="genderLabel">Masculino</label>
</div>
Could someone guide me through the problem? I can't find where is my error in the JS code. Thank you
The ids must be unique so what Spring is doing is just fine. Just use the name attribute for the selector instead of the id.
$('input[name="userDto\\.gender"]').change( ... )