Show DIV only when a specific radio item is selected - html

I have this code which works without a problem with select options:
HTML
<select onchange="java_script_:show(this.options[this.selectedIndex].value)">
<option value="Apple">Apple</option>
<option value="Orange">Orange</option>
<option value="Bananna">Bananna</option>
</select>
<div id="hiddenDiv" style="display:none">Hidden Question about Oranges</div>
Script
<script>
function show(aval) {
if (aval == "Orange") {
hiddenDiv.style.display='inherit';
} else {
hiddenDiv.style.display='none';
}
}
</script>
Any ideas how can I make this code to work if those were radio options?

Try it like this
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<form id="myForm">
<input type="radio" name="radioName" id="Apple"/> Apple <br>
<input type="radio" name="radioName" id="Orange" /> Orange <br>
<input type="radio" name="radioName" id="Bananna" /> Bananna <br>
</form>
<div id="hiddenDiv" style="display:none">Hidden Question about Oranges</div>
<script>
$('#myForm input').on('change', function() {
if ($(this).attr('id') == "Orange") {
hiddenDiv.style.display='inherit';
} else {
hiddenDiv.style.display='none';
}
});
</script>

My solution with JQuery:
$(function () {
$("select").click(function () {
var validOption = $("select > option[value='Orange']");
if (validOption[0].selected) {
$("#hiddenDiv").show();
} else {
$("#hiddenDiv").hidde();
}
});
});

Related

Check a radio button by default with jQuery

in an html page that I use to display text snippets, I use jQuery to filter those that contain just text, text including some url links or both :
JS code :
$(document).ready(function() {
$('input[type=radio]').attr('checked', false);
$(".selection").on("change", ":radio", function() {
var checkedVal = parseInt($(this).filter(":checked").val(), 10);
$(".container").hide().filter(function() {
if (checkedVal === 2) {
return $(this).find('a[href]').length && $(this)
} else if (checkedVal === 1) {
return $(this).find('a[href]').length <= 0 && $(this)
} else {
return $(this)
}
}).show();
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="selection">
</button> Display : <input type="radio" name="check" value="1" /> Texts only | <input type="radio" name="check" value="2" /> Texts w/ links | <input type="radio" name="check" value="-1" checked="checked" /> Texts + Texts w/ links</div>
I would like the "Texts + Texts w/ links" button to be checked by default and this choice to be kept when the page is refreshed.
I don't know how to do this. Any help would be greatly appreciated, thank you.
Instead of unchecking all the buttons, check the one you want to be the default.
$(document).ready(function() {
$('.selection :radio[value=-1]').attr('checked', true);
$(".selection").on("change", ":radio", function() {
var checkedVal = parseInt($(".selection :radio:checked").val(), 10);
$(".container").hide().filter(function() {
if (checkedVal === 2) {
return $(this).find('a[href]').length && $(this)
} else if (checkedVal === 1) {
return $(this).find('a[href]').length <= 0 && $(this)
} else {
return $(this)
}
}).show();
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="selection">
</button> Display : <input type="radio" name="check" value="1" /> Texts only | <input type="radio" name="check" value="2" /> Texts w/ links | <input type="radio" name="check" value="-1" checked="checked" /> Texts + Texts w/ links</div>

Show DIV if two input values have the same value with jQuery

I am trying to show a DIV if two inputs have the same value, as already asked here: How to compare two inputs and show a div
I can run the code snippet, but when I try it myself it doesn't seem to work. The code is very simple, yet I don't see why the DIV isn't showing when the two values are matching:
$("#some1, #some2").on("keyup change", function(){
let firstEl = $("#some1"),
secondEl = $("#some2"),
conditionalEl = $("#showhide");
if (firstEl.val() == secondEl.val() ) {
conditionalEl.show();
} else {
conditionalEl.hide();
}
});
#showhide {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<input type="number" name="some1" id="some1">
<input type="number" name="some2" id="some2">
<div id="showhide">The inputs are the same</div>
<input type="submit">
</form>
</html>
Seems your key up function is not working, Try this
<html>
<head>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
function myFunction(vaule){
let conditionalEl = $('#showhide');
if ($("#some1").val() == $("#some2").val() ) {
conditionalEl.show();
} else {
conditionalEl.hide();
}
}
</script>
<form>
<input type="number" name="some1" id="some1" onchange="myFunction()">
<input type="number" name="some2" id="some2" onchange="myFunction()">
<div id="showhide" style="display:none;">The inputs are the same</div>
<input type="submit">
</form>
</body>
</html>

How to hide a label of a dropdown with hide() function

I have 2 dropdowns (EmployeeType, Type) and a textbox (Rate) and I just want to hide "Type" and "Rate" according to the selected value of "EmployeeType". I've done it with no issues to the "Rate", but for the "Type" only the dropdown hides not the label. Can anyone help me to hide the label of the dropdown as well?
<div class="control-group">
<label class="control-label" for="Designation">Employee Type</label>
<div class="controls">
<select name="Designation" onchange="ajaxRate(this)" id="Designation">
<option value="">--Select Employee Type--</option>
<option value="Center">Center</option>
<option value="Visiting">Visiting</option>
<option value="Other">Other</option>
</select>
<span id="ajax_img3"></span>
</div>
<!-- designation-->
<br/>
<!-- 2019-03-18 Visiting Type -->
<div class="control-group">
<label class="control-label" for="Type">Type</label>
<div class="controls">
<select name="Type" id="TypeValue">
<option value="">--Select Type--</option>
<option value="Yes">Regular</option>
<option value="No">Visiting</option>
</select>
</div>
</div>
<div class="control-group" id='rate'>
<label class="control-label" for="Rate">Hourly Rate</label>
<div class="controls">
<input type="number" name="Rate" id='rateValue'/>
</div>
</div>
#include('includes.footer')
<script>
$(document).ready(function () {
$("#rate").hide();
// $("#Type").hide();
});
function ajaxRate(x) {
if (x.value == 'Center') {
$("#rate").hide();
$("#rateValue").val('0');
$("#TypeValue").show();
} else {
$("#rate").show();
$("#rateValue").val('0');
$("#TypeValue").hide();
}
}
$( document ).ready(function()
{
$("#rate").hide();
// $("#Type").hide();
});
function ajaxRate(x)
{
if(x.value=='Center')
{
$("#rate").hide();
$("#rateValue").val('0');
$("#TypeValue").show();
$("#TypeValue").parents(".control-group").show();
}
else
{
$("#rate").show();
$("#rateValue").val('0');
$("#TypeValue").hide();
$("#TypeValue").parents(".control-group").hide();
}
}
You have to hide full div using parents or you have to add any id to main div to hide.
function ajaxRate(x) {
if(x.value=='Center')
{
$("#rate").hide();
$("#rateValue").val('0');
$("#TypeValue").closest('div.control-group').show();
}
else
{
$("#rate").show();
$("#rateValue").val('0');
$("#TypeValue").closest('div.control-group').hide();
}
}

jquery validate errorPlacement in select2.js [duplicate]

This question already has answers here:
select2 and jquery validation not working properly
(3 answers)
How to make select2 work with jquery.validation plugin?
(15 answers)
Error placement, Select2
(2 answers)
Closed 4 years ago.
When error replacement in select2 is showing up above the box, I want the error replacement below the box. Here is my HTML:
<form id="form" class="form_required">
<select type="text" id="test1" class="form-control select2" required>
<option value="">&nbsp();</option>
<option value="1">1</option>
</select>
<button type="button" onclick="insert()">Submit</button>
</form>
Here is the JavaScript:
$('.select2').select2()
$(".form_required").validate({
highlight: function (element, errorClass, validClass) {
$(element).parents('.form-control').removeClass('has-success').addClass('has-error');
},
unhighlight: function (element, errorClass, validClass) {
$(element).parents('.form-control').removeClass('has-error').addClass('has-success');
},
errorPlacement: function (error, element) {
if (element.parent('.input-group').length) {
error.insertAfter(element.parent());
}
else if (element.prop('type') === 'radio' && element.parent('.radio-inline').length) {
error.insertAfter(element.parent().parent());
}
else if (element.prop('type') === 'checkbox' || element.prop('type') === 'radio') {
error.appendTo(element.parent().parent());
}
else {
error.insertAfter(element);
}
}
});
function insert(){
if ($('#form').valid()){
alert('OK');
}
}
1) 2)
When not using select2 the error replacement is showing up below box the like number 2 but when using select2 error replacement is showing up above the box..
If you check the source code of the select2 library, you'll see that the select2-container (container responsible for the select2 dropdown) is inserted right after the select element. Here's the part of the code which does that:
Select2.prototype._placeContainer = function ($container) {
$container.insertAfter(this.$element);
var width = this._resolveWidth(this.$element, this.options.get('width'));
if (width != null) {
$container.css('width', width);
}
};
which gets us to easily place the error message from the jquery-validate after this select2-container. Here's the change in the errorPlacement option:
errorPlacement: function (error, element) {
if(element.hasClass('select2') && element.next('.select2-container').length) {
error.insertAfter(element.next('.select2-container'));
}
....
And a snippet showing the results:
$('.select2').select2({container: 'body'})
$(".form_required").validate({
highlight: function (element, errorClass, validClass) {
$(element).parents('.form-control').removeClass('has-success').addClass('has-error');
},
unhighlight: function (element, errorClass, validClass) {
$(element).parents('.form-control').removeClass('has-error').addClass('has-success');
},
errorPlacement: function (error, element) {
if(element.hasClass('select2') && element.next('.select2-container').length) {
error.insertAfter(element.next('.select2-container'));
} else if (element.parent('.input-group').length) {
error.insertAfter(element.parent());
}
else if (element.prop('type') === 'radio' && element.parent('.radio-inline').length) {
error.insertAfter(element.parent().parent());
}
else if (element.prop('type') === 'checkbox' || element.prop('type') === 'radio') {
error.appendTo(element.parent().parent());
}
else {
error.insertAfter(element);
}
}
});
form {
margin: 20px;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/css/select2.min.css">
<script src="https://code.jquery.com/jquery-3.2.1.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.full.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/jquery.validate.min.js"></script>
<form id="form" class="form_required form-horizontal">
<div class="form-group">
<div class="col-sm-8">
<select type="text" id="test1" class="form-control select2" required>
<option value=""> </option>
<option value="1">test1</option>
</select>
</div>
</div>
<div class="form-group">
<button type="button" onclick="insert()">Submit</button>
</div>
</form>
<form id="form2" class="form_required form-horizontal">
<div class="form-group">
<div class="col-sm-8">
<select type="text" id="test2" class="form-control" required>
<option value=""> </option>
<option value="1">test2</option>
</select>
</div>
</div>
<div class="form-group">
<button type="button" onclick="insert2()">Submit</button>
</div>
</form>
<script>
function insert(){
if ($('#form').valid()){
alert('OK');
}
}
function insert2(){
if ($('#form2').valid()){
alert('OK');
}
}
</script>
Hope this helps.

How to add a class to a div on a certain option value

I was wondering how to add a class to a div if the option has the value selected. For example I'm making a website for my cousin's business, we want an easy way to shuffle through menus.
<code>
<div id="select"><form>
Menu:
<select name="select" class="form-control">
<option value="drinks">Drinks</option>
<option value="breakfast">Breakfast</option>
</select>
<input type="submit" value="Submit">
</form>
</code>
I would like if the value is breakfast to hide the div drinks and show the div breakfast, and also have it reversible. Thank you here's my code on JSFiddle http://jsfiddle.net/t5R9s/
Here it is
$(document).ready(function() {
$(".form-control").change(function() {
if($(this).val() == "drinks") {
$("#drinks").show();
$("#breakfast").hide();
} else {
$("#drinks").hide();
$("#breakfast").show();
}
});
});
Here is the FIDDLE.
You can use javascript. Do you want to do it when you hit submit or just after you make the selection?
[
Fiddle
]
<script type="text/javascript">
function changeClass(){
var myVariable = document.getElementById('control').value;
if(myVariable == "drinks"){
document.getElementById('drinks').className = "";
document.getElementById('breakfast').className = "hide";
}
else if(myVariable == "breakfast"){
document.getElementById('drinks').className = "hide";
document.getElementById('breakfast').className = "";
}
}
</script>
<div id="select">
<form>Menu:
<select name="select" class="form-control" id="control" onChange="changeClass()">
<option value="drinks">Drinks</option>
<option value="breakfast">Breakfast</option>
</select>
</form>
</div>
<div id="drinks">
<h2 align="center">Drinks</h2>
<table width="888" border="" cellpadding="9" class="">..</table>
</div>
<!--Breakfast table-->
<div id="breakfast" class="hide">
<h2 align="center">Breakfast</h2>
<table width="888" border="" cellpadding="9" class="">..</table>
</div>