I need to know if two options from a select dropdown list have been chosen so I can take the text and make a button that has that text inside of it show up. Right now the button shows with some text before the options have been clicked but doesn't show up again if closed and options have been chosen.
If someone has answered this question please let me know I couldn't find anything to help me.
Here's my code:
$(document).ready(function () {
var valueFrom = $('#revenueFrom option:selected').text();
var valueTo = $('#revenueTo option:selected').text();
if ($('#revenueFrom option').data('clicked', true) && $('#revenueTo option').data('clicked', true)) {
$('#annual-revenue-button').text("");
$('#annual-revenue-button').append(valueFrom + "To:" + valueTo + " " + "×");
$('#annual-revenue-button').show('fast');
};
});
$('.search-popup').click(function () {
$(this).hide('fast');
});
button{
background-color:whitesmoke;
border-radius: 20px;
display:none;
}
button:hover{
background-color:lightgray;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<button class="search-popup btn" id="annual-revenue-button" type="button"></button>
<form class="form-inline revenue">
<div class="form-group">
<label>Annual Revenue</label>
<select name="revenueFrom" class="form-control" id="revenueFrom">
<option value="" selected disabled>From:</option>
<option value="0">$0</option>
<option value="1">$500,000</option>
<option value="2">$1 Million</option>
<option value="3">$2.5 Million</option>
<option value="4">$5 Million</option>
</select>
<select name="revenueTo" class="form-control to" id="revenueTo">
<option value="" selected disabled>To:</option>
<option value="0">$0</option>
<option value="1">$500,000</option>
<option value="2">$1 Million</option>
<option value="3">$2.5 Million</option>
<option value="4">$5 Million</option>
</select>
</div>
</form>
You need to set a .change() event listener on the select elements.
When one of them has been changed, check the values of those select elements are not empty, and then show your button.
$(document).ready(function () {
$('#revenueFrom, #revenueTo').change(function(){
if ($('#revenueFrom').val() && $('#revenueTo').val()) {
var valueFrom = $('#revenueFrom option:selected').text();
var valueTo = $('#revenueTo option:selected').text();
$('#annual-revenue-button').html("From: " + valueFrom + " To: " + valueTo + " ×").show('fast');
};
});
});
$('.search-popup').click(function () {
$(this).hide('fast');
});
button{
background-color:whitesmoke;
border-radius: 20px;
display:none;
}
button:hover{
background-color:lightgray;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<button class="search-popup btn" id="annual-revenue-button" type="button"></button>
<form class="form-inline revenue">
<div class="form-group">
<label>Annual Revenue</label>
<select name="revenueFrom" class="form-control" id="revenueFrom">
<option value="" selected disabled>From:</option>
<option value="0">$0</option>
<option value="1">$500,000</option>
<option value="2">$1 Million</option>
<option value="3">$2.5 Million</option>
<option value="4">$5 Million</option>
</select>
<select name="revenueTo" class="form-control to" id="revenueTo">
<option value="" selected disabled>To:</option>
<option value="0">$0</option>
<option value="1">$500,000</option>
<option value="2">$1 Million</option>
<option value="3">$2.5 Million</option>
<option value="4">$5 Million</option>
</select>
</div>
</form>
You need to add Multiple and add [] after name=revenueFrom So an array with all of the elements is being passed on form submission.
This post may also be of use to you how to access multiple select array data in javascript
Note this function:
function getSelectedOptions(element) {
// validate element
if(!element || !element.options)
return []; //or null?
// return HTML5 implementation of selectedOptions instead.
if (element.selectedOptions)
return element.selectedOptions;
// you are here because your browser doesn't have the HTML5 selectedOptions
var opts = element.options;
var selectedOptions = [];
for(var i = 0; i < opts.length; i++) {
if(opts[i].selected) {
selectedOptions.push(opts[i]);
}
}
return selectedOptions;
}
It would also be possible to make an event listener that looks for change on the select and appends a new element somewhere else with the selected data if that's what you needed, feel free to update your question if you need a more specific answer.
Related
I had a challenge getting this question but tried to research and redo it.
I'm trying to get an item in a list from a controller, then iterate through the list. Based on the content of the array, I would like to show or hide the select that has options in it. I can't seem to hide or show any of them at the moment.
var names = ['marketing']; //or ['business']
var text = "";
var i;
//$(document).ready(function() {
for (i = 0; i < names.length; i++) {
if (names[i] === 'business') {
//alert('Hooray');
$("#business").show("slow");
$(".marketing").hide("fast");
} else
if (names[i] === 'marketing') {
$("#marketing").show("slow");
$(".marketing").hide("fast");
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="all" id="note">
Some content will be posted here...
</div>
<select id="marketing" multiple>
<option value="1">Definition</option>
<option value="2">Advertise</option>
<option value="3">Promotion</option>
</select>
<select id="business" multiple>
<option value="1">Definition</option>
<option value="2">Buy</option>
<option value="3">Sell</option>
</select>
If your array contains the exact name of the id you can "hide" the elements with CSS and show them with two lines of javascript
var names = ['marketing'];//or ['business']
names.forEach( name => {
$('#' + name).show('slow')
});
select {
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="all" id="note">
Some content will be posted here...
</div>
<select id="marketing" multiple>
<option value="1">Definition</option>
<option value="2">Advertise</option>
<option value="3">Promotion</option>
</select>
<select id="business" multiple>
<option value="1">Definition</option>
<option value="2">Buy</option>
<option value="3">Sell</option>
</select>
I'm using the following code to pass a value from a select option to a given input field.
$(".itemconditionmenu").change(function() {
var conditionofitem = $(this).val();
$("#meta_itemcondition").val(conditionofitem);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<select id="itemcondition" class="itemconditionmenu">
<option value="">Choose Condition</option>
<option value="New">New</option>
<option value="Used">Used</option>
</select>
<input id="meta_itemcondition" type="text" value="New">
But how can I pass the value of the input field back to the select option value using jQuery?
I am not sure what you are exactly looking, however, as per my understanding I have modifed your code samples.. You can check below snippet.
$( document ).ready(function() {
var conditionofitem = $("#meta_itemcondition").val();
$(".itemconditionmenu").val(conditionofitem);
});
$(".itemconditionmenu").change(function() {
var conditionofitem = $(this).val();
if(conditionofitem == '') {
$("#meta_itemcondition").val('Choose Condition');
} else {
$("#meta_itemcondition").val(conditionofitem);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<select id="itemcondition" class="itemconditionmenu">
<option value="">Choose Condition</option>
<option value="New">New</option>
<option value="Used">Used</option>
</select>
<input id="meta_itemcondition" type="text" value="New">
Please check and let me know if you have any further queries.
Try this
$("#addOption").click(function(){
$("#itemconditionmenu").append('<option value="'+$("#meta_itemcondition").val()+'" selected="selected">'+$("#meta_itemcondition").val()+'</option>');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="itemconditionmenu">
<option value="1">1</option>
<option value="2">2</option>
</select>
<input id="meta_itemcondition" type="text" value=""></input>
<button id="addOption">ADD</button>
Example for the same: Blur out your input and value option will be added to select dropdown
$(".itemconditionmenu").change(function() {
var conditionofitem = $(this).val();
$("#meta_itemcondition").val(conditionofitem);
});
$("#meta_itemcondition").blur(function() {
var inputValue = $(this).val();
var o = new Option(inputValue, inputValue);
$(o).html(inputValue);
$("#itemcondition").append(o);
$("#itemcondition").val(inputValue);
});
$( document ).ready(function() {
var value = $("#meta_itemcondition").val();
console.log(value)
var alreadyOption = false;
$("#itemcondition option").each(function()
{
alreadyOption = $(this).val() === value;
if(alreadyOption) {
$("#itemcondition").val(value);
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<select id="itemcondition" class="itemconditionmenu">
<option value="">Choose Condition</option>
<option value="New">New</option>
<option value="Used">Used</option>
</select>
<input id="meta_itemcondition" type="text" value="New"/>
Here you can find inline functionality
if you enter a value in textbox value append in drop-down
if you select drop-down value auto set in textbox.
Note: add value in textbox change just input focus
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select class="jsSelect" onchange="$('.jsTextBox').val($(this).val())">
<option value="1">1</option>
<option value="2">2</option>
</select>
<input class="jsTextBox" onchange="if($(this).val()!=''){$('.jsSelect').append($('<option></option>').attr('value',$(this).val()).text($(this).val()));$('.jsSelect').val($(this).val());}" type="text" value="">
How to take the values of value1 and value2 in two variables using javascript?
<select class="form-control" id="country" name="country">
<option value="**value1**" "**value2**" >Select Item</option>
</select>
You could make your own attribute. I know you probably do not want to get the element with an ID, but I don't know the context. You can just call getAttribute on the option and use any name you gave to the "custom" attribute.
window.addEventListener('load', ()=>
{
const option = document.getElementById('option');
function init()
{
//Use this to get the values
console.log(option.getAttribute('other-value'));
}
init();
});
<select class="form-control" id="country" name="country">
<option id="option" value="value1" other-value="value2">Select Item</option>
</select>
I don't know what you really want to do with your piece of code,
but here is a proper way to use the option elements, and a way to split multiple values with a fixed separator:
var example = document.getElementById('example');
example.addEventListener('change', function() {
// Console displays the “value”, and not the text, of the selected option
console.log("option value:", example.value);
});
// Here is what I'll do with your "multiple" values
var country = document.getElementById('country');
var options = country.querySelector('option');
var values = options.value.split("/");
values.forEach(function(val) {
country.innerHTML += "<option value=" + val + ">" + val + "</option>";
});
<p>My simple example</p>
<select id="example" name="country">
<option value="--">Select Country</option>
<option value="GB">Great Britain</option>
<option value="FR">France</option>
</select>
<br>
<br>
<p>Example with option getting splitted</p>
<select class="form-control" id="country" name="country">
<!-- Let's say your multiple values are separated by a "/" -->
<option value="**value1**/**value2**">Select Item</option>
</select>
Hope it helps.
$('select').on('change', function() {
console.log( $('#country').val() );
console.log($(this).find(':selected').data('second'));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select class="form-control" id="country" name="country">
<option value="value1" data-second ="value2" >Select Item 1</option>
<option value="value3" data-second ="value4" >Select Item 2</option>
</select>
I have several selects in my form. If one select has had its options changed then it should increase the counter. 5 selects then counter should be 5. Essentially I want to check if all my selects have been selected so I can submit the form. I don't want to use "required".
<div id="firstPanelID"
<div class="form-group input-group">
<label class="fixingLabelAlignmentInner">Transfer Code of Center from which Infant Transferred : </label>
<div class="fixingInputAlignmentInner">
<select id="transferCodePIW" name=transferCodePIW class="form-control" style="height:32px;width:80%;">
<option disabled selected value>SELECT</option>
<option value="13240">13240 - Mowbray Maternity Hospital</option>
<option value="14994">14994 - New Somerset Hospital</option>
<option value="16011">16011 - Tygerberg Hospital</option>
<option value="8005432">8005432 - Khayelitsha District Hospital</option>
<option value="8005433">8005433 - Michell's Plain District Hospital</option>
<option value="8005435">8005435 - Red Cross Children's Hospital</option>
<option value="97777777">97777777 - Birth at Home or in Transit</option>
<option value="">Other</option>
<option value="77777777">N/A</option>
<option value="99999999">Unknown</option>
</select>
</div>
</div>
</div>
I have tried it with the following. The selects may be visible/invisible due to other radio buttons hence the visible. The function works for radio buttons but I can't get it to work for selects.
var sgroups = [];
$('#firstPanelID select:visible:selected').each(function(index, el){
var i;
for(i = 0; i < sgroups.length; i++)
if(sgroups[i] == $(el).attr('name'))
return true;
sgroups.push($(el).attr('name'));
}
);
alert(($('#firstPanelID select:visible:selected').length))
I think the code is self-explanatory.
var counter = 0;
$('select').change(function () {
var o = $(this);
if (!o.hasClass('counted')) {
counter++;
o.addClass('counted');
}
$('#counter').text(counter);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
<option value="aa">aa</option>
<option value="bb">bb</option>
<option value="cc">cc</option>
</select>
<select>
<option value="2aa">2aa</option>
<option value="2bb">2bb</option>
<option value="2cc">2cc</option>
</select>
<select>
<option value="3aa">3aa</option>
<option value="3bb">3bb</option>
<option value="3cc">3cc</option>
</select>
<div id="counter">
0
</div>
If you want to check if all selects have been checked, assuming each select will have an empty value, you can do this:
function isThereAnyEmptySelection () {
let r = false;
$('select').each(function () {
if ($(this).val() === '') { // or === null, or === undefined, or === '0' etc., depending on context
r = true;
}
});
return r;
}
You can use .one() to attach a single change event to each <select> element, when count is equal to $("selector").length call .off() to remove change event, then perform action, for example, submit <form> element.
let count = 0;
$("select").one("change", function() {
++count;
console.log(count);
if (count === $("select").length) {
$("select").off("change");
// submit form here
console.log(count + " is " + $("select").length)
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
<option value="1">1</option>
<option value="2">2</option>
</select>
<select>
<option value="1">1</option>
<option value="2">2</option>
</select>
<select>
<option value="1">1</option>
<option value="2">2</option>
</select>
<select>
<option value="1">1</option>
<option value="2">2</option>
</select>
<select>
<option value="1">1</option>
<option value="2">2</option>
</select>
I am facing a issue that I want the select value on click event. i tried with change event but I get all values comma separated instead of current value.
What I want to do is, Suppose there are multiple options with a All option. So if a user select All option then it should clear all selected option and only All option will display.
I have tried below codes
$(".chzn-select").on('select2:selecting', function () {
var st = ($(this).select2('val'));
}
it show null on first click.
And
$(".chzn-select").on('change', function () {
var st = ($(this).select2('val'));
}
show all values in comma separated format. There is no example I found of onclick event with select2 JS. Here is the list of events
HTML
<select name="country[]" multiple="" data-placeholder="Select country" class="form-control chzn-select" required="required" id="country">
<option disabled="disabled" value="1">Abu Dhabi</option>
<option value="2">Dubai</option>
<option disabled="disabled" value="3">Singapore</option>
<option disabled="disabled" value="4">Thailand</option>
<option value="8">Afghanistan</option>
<option value="All">All</option>
</select>
Here is the jsfiddle
https://jsfiddle.net/bayahiassem/gtew005w/11/
$(".chzn-select").select2({selectOnClose: true}).on("select2:selecting", function(evt) {
if(evt.params.args.data.id == 'All') {
$(".chzn-select").val(null).trigger("change");
} else {
$('.chzn-select > option[value=All]').prop("selected", false);
}
});
I didn't get you.
If i correct.
$(this).select2('val').find(':selected');
Here you are. It will you give the current selected item(s) - hold ctrl for multiple selections
$(function () {
$(".chzn-select").on('change', function (evt) {
$('#selected').text('Selected: ' + ($(this).val()))
})
})
select {
width: 50%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="country[]" multiple="" data-placeholder="Select country" class="form-control chzn-select" required="required" id="country">
<option disabled="disabled" value="1">Abu Dhabi</option>
<option value="2">Dubai</option>
<option disabled="disabled" value="3">Singapore</option>
<option disabled="disabled" value="4">Thailand</option>
<option value="8">Afghanistan</option>
<option value="All">All</option>
</select>
<div id="selected"></div>
I hope this will work,
$(".chzn-select").select2({selectOnClose: true});
$(".chzn-select").on("select2:select", function (evt) {
if($.inArray('All',$(this).val()) != -1){
$(this).val('All').trigger("change");
}
});
Replace your js code with this, it will work.
Here is jsfiddle link for working code.