jQuery chosen change display 2 level after select - javascript

How would I be able to display select menu when someone select the value option in style id? Code is like this.
HTML :
<div class="row">
<select name="style" id="style">
<option value="banner"></option>
<option value="text"></option>
<option value="vedio"></option>
</select>
</div>
<div class="row">
<select name="language" id="language" style="display:none;">
<option value="EN"></option>
<option value="ES"></option>
<option value="FR"></option>
</select>
</div>
<div class="row">
<select name="english" id="english" style="display:none;">
<option value="text"></option>
<option value="img"></option>
<option value="vedio"></option>
</select>
</div>
<div class="row">
<select name="spanish" id="spanish" style="display:none;">
<option value="text"></option>
<option value="img"></option>
<option value="vedio"></option>
</select>
</div>
<div class="row">
<select name="french" id="french" style="display:none;" >
<option value="text"></option>
<option value="img"></option>
<option value="vedio"></option>
</select>
</div>
SCRIPT :
$('#style').change(function(){
selection = $(this).val();
switch(selection)
{
case 'text':
$('#language').show();
break;
default:
$('#language').hide();
break;
}
});
$('#language').change(function(){
selection = $(this).val();
switch(selection)
{
case 'EN':
$('#english').show();
break;
default:
$('#english').hide();
break;
}
});
i want select text value i get language choice when i select language i get anther select menu

You are using the id selector $('#language') and your selects have no ids. You should give ids to each of your selects or use the attribute-equals selector $('[name="language"]') instead.

Try changing
selection = $(this).val();
to
selection = $('#style option:selected').val();

Related

How to get select value for AJAX using JavaScript

I have 2 HTML drop down menus.
<div class="bar-option">
<label>Filter:</label>
<form>
<select name="retreat_locations" onchange="filterRetreats(this.value, 'count(reviews.retreat_id)')">
<option value="alllocations" selected="selected">All Locations</option>
<?php foreach ($this->locations as $location) {?>
<option value="<?=htmlentities($location->retreat_location);?>">
<?=htmlentities($location->retreat_location);?> </option>
<?php }?>
</select>
</form>
</div>
<div class="bar-option">
<label>Sort by:</label>
<select onchange="filterRetreats('alllocations', this.value)">
<option selected='selected' value='retreat_number_of_reviews'>Popularity</option>
<option value='retreat_founded_in'>Age</option>
<option value='total_review_cost'>Cost</option>
</select>
</div>
As you can see from the above, the second select uses the static value 'alllocations' as the first parameter for the onchange function.
How can I replace this static value with the selected option of the first dropdown (retreat_locations)?
I tried retreat_locations.value but it didn't work.
Any help appreciated.
You can use:
document.querySelector('[name=retreat_locations]').value
Your code will become:
filterRetreats(document.querySelector('[name=retreat_locations]').value, this.value)
Demo:
function filterRetreats(value1, value2) {
console.log(value1, value2);
}
<div class="bar-option">
<label>Filter:</label>
<form>
<select name="retreat_locations" onchange="filterRetreats(this.value, 'count(reviews.retreat_id)')">
<option value="alllocations" selected="selected">All Locations</option>
<option value="2">2</option>
</select>
</form>
</div>
<div class="bar-option">
<label>Sort by:</label>
<select onchange="filterRetreats(document.querySelector('[name=retreat_locations]').value, this.value)">
<option selected='selected' value='retreat_number_of_reviews'>Popularity</option>
<option value='retreat_founded_in'>Age</option>
<option value='total_review_cost'>Cost</option>
</select>
</div>
Other -old school- option is to add an id tag to the first select, e.g. id="retreat_locations" and use document.getElementById('retreat_locations').value.

Show hidden select menus if value of another select menu is selected

I am new to jQuery and javaScript. I am using bootstrap. I have a select menu that is visible and two additional select menus that are hidden. I would like to show the hidden select menus id the user selects "TV" from the visible select menu options. If they select any of the other value options from the visible select menu, I need to display a simple text box where they can explain. After researching online I attempted to do it using some js, but it is not working. Below is my code and here is my jsfiddle link: https://jsfiddle.net/nx6cc1Lc/
HTML:
<div class="hear-from">
<div class="selects-4 col-xs-12">
<label for="heard_tv">Where did you hear about us from?</label>
<select id="heard_tv" class="form-control selectTV" name="heard_tv">
<option>--Choose Option--</option>
<option value="TV">TV Commercial</option>
<option value="Radio">Radio Advertisement-Other</option>
<option value="OT">Other</option>
</select>
</div>
<div class="selects-5 col-xs-6 hidden">
<select id="heard_from_station" class="form-control selectStation" name="heard_from_station">
<option>--Choose Station--</option>
<option value="TV:ABC">ABC News</option>
<option value="TV:TWCNews">New York 1 - TWC News</option>
<option value="TV:BBC">BBC America</option>
<option value="TV:CNBC">CNBC</option>
<option value="TV:CNN">CNN</option>
<option value="TV:Fox News">FOX News</option>
<option value="TV:Fox Business">FOX Business</option>
<option value="TV:TWCNews">Time Warner News</option>
<option value="TV:HLN">Headline News</option>
<option value="TV:MSNBC">MSNBC</option>
<option value="TV:Other">Other</option>
</select>
</div>
<div class="selects-6 col-xs-6 hidden">
<select id="heard_from_provider" class="form-control selectProvider" name="heard_from_provider">
<option>--Choose Provider--</option>
<option value="TVP:ATT">AT & T</option>
<option value="TVP:Comcast">Comcast</option>
<option value="TVP:Cablevision">Cablevision</option>
<option value="TVP:Charter">Charter Comm.</option>
<option value="TVP:Cox">Cox Comm.</option>
<option value="TVP:DirectTV">DirectTV</option>
<option value="TVP:Dish">Dish Network</option>
<option value="TVP:TimeWarner">Time Warner Cable</option>
<option value="TVP:VerFiOS">Verizon FiOS</option>
<option value="TVP:Antenna">Over the Air / Antenna</option>
<option value="TVP:Other">Other TV Provider</option>
</select>
</div>
</div>
Javascript:
// show tv station and provider menus if TV selected
$(document).ready(function(){
$('#heard_tv').on('change', function() {
if ( this.value === "TV")
//.....................^.......
{
$("#heard_from_station").removeClass('hidden');
$("#heard_from_provider").removeClass('hidden');
}
else
{
$('#heard_from_station').removeClass().addClass('hidden');
$('#heard_from_provider').removeClass().addClass('hidden');
}
});
});
Update your script as given below.
// show tv station and provider menus if TV selected
$(document).ready(function(){
$('#heard_tv').on('change', function() {
if ( this.value === "TV")
{
$("#heard_from_station").parent().removeClass('hidden');
$("#heard_from_provider").parent().removeClass('hidden');
}
else
{
$('#heard_from_station').parent().addClass('hidden');
$('#heard_from_provider').parent().addClass('hidden');
}
});
});
As your html contains "hidden" class on the parent element ".parent()", you need to add / remove class on the parent. Alternatively, you can also give ID to the parent elements and use those ids directly in your script, without using ".parent()".
Just use show() and hide() of jquery for this.Below is the code part for your issue
<script src="jquery.min.js"></script>
<script src="sample.js"></script>
<div class="hear-from">
<div class="selects-4 col-xs-12">
<label for="heard_tv">Where did you hear about us from?</label>
<select id="heard_tv" class="form-control selectTV" name="heard_tv">
<option>--Choose Option--</option>
<option value="TV">TV Commercial</option>
<option value="Radio">Radio Advertisement-Other</option>
<option value="OT">Other</option>
</select>
</div>
<div class="selects-5 col-xs-6" style="display:none">
<select id="heard_from_station" class="form-control selectStation" name="heard_from_station">
<option>--Choose Station--</option>
<option value="TV:ABC">ABC News</option>
<option value="TV:TWCNews">New York 1 - TWC News</option>
<option value="TV:BBC">BBC America</option>
<option value="TV:CNBC">CNBC</option>
<option value="TV:CNN">CNN</option>
<option value="TV:Fox News">FOX News</option>
<option value="TV:Fox Business">FOX Business</option>
<option value="TV:TWCNews">Time Warner News</option>
<option value="TV:HLN">Headline News</option>
<option value="TV:MSNBC">MSNBC</option>
<option value="TV:Other">Other</option>
</select>
</div>
<div class="selects-6 col-xs-6" style="display:none">
<select id="heard_from_provider" class="form-control selectProvider" name="heard_from_provider">
<option>--Choose Provider--</option>
<option value="TVP:ATT">AT & T</option>
<option value="TVP:Comcast">Comcast</option>
<option value="TVP:Cablevision">Cablevision</option>
<option value="TVP:Charter">Charter Comm.</option>
<option value="TVP:Cox">Cox Comm.</option>
<option value="TVP:DirectTV">DirectTV</option>
<option value="TVP:Dish">Dish Network</option>
<option value="TVP:TimeWarner">Time Warner Cable</option>
<option value="TVP:VerFiOS">Verizon FiOS</option>
<option value="TVP:Antenna">Over the Air / Antenna</option>
<option value="TVP:Other">Other TV Provider</option>
</select>
</div>
</div>
<script> $(document).ready(function(){
$('#heard_tv').on('change', function() {
if ( this.value === "TV")
//.....................^.......
{
$(".selects-5").show();
$(".selects-6").show();
}
else
{
$('.selects-5').hide();
$('.selects-6').hide();
}
});
});</script>
The problem is that the div tags that contain your secondary select menus are hidden, not the select menus themselves -- even so, your script is trying to remove the class from the select menus. So there are a number of ways you can fix this.
One way to fix this is to apply the hidden class to your select menus instead of your div tags.
<div class="selects-5 col-xs-6">
<select id="heard_from_station" class="form-control selectStation hidden" name="heard_from_station">
...
</select>
</div>
<div class="selects-6 col-xs-6">
<select id="heard_from_provider" class="form-control selectProvider hidden" name="heard_from_provider">
...
</select>
</div>

In several selects make selected options uniq

I have question where you need to find pairs of words in Russian and English
<div class="form-group" id="question4">
<label for="q4FirstSelectEN">4</label>
<div class="row">
<div class="col-lg-offset-2 col-lg-2 q4EN">
<select name="firstSelectEn" id="q4FirstSelectEN">
<option disabled selected style="display: none" value=""></option>
<option value="red">red</option>
<option value="green">green</option>
<option value="purple">purple</option>
</select>
<select class="top-buffer" name="secondSelectEn" id="q4SecondSelectEN">
<option disabled selected style="display: none" value=""></option>
<option value="red">red</option>
<option value="green">green</option>
<option value="purple">purple</option>
</select>
<select class="top-buffer" name="secondSelectEn" id="q4ThirdSelectEN">
<option disabled selected style="display: none" value=""></option>
<option value="red">red</option>
<option value="green">green</option>
<option value="purple">purple</option>
</select>
</div>
<div class="col-lg-2 q4RU">
<select name="firstSelectRu" id="q4FirstSelectRu">
<option disabled selected style="display: none" value=""></option>
<option value="red">красный</option>
<option value="green">зелёный</option>
<option value="purple">фиолетовый</option>
</select>
<select class="top-buffer" name="firstSelectRu" id="q4SecondSelectRu">
<option disabled selected style="display: none" value=""></option>
<option value="red">красный</option>
<option value="green">зелёный</option>
<option value="purple">фиолетовый</option>
</select>
<select class="top-buffer" name="firstSelectRu" id="q4ThirdSelectRU">
<option disabled selected style="display: none" value=""></option>
<option value="red">красный</option>
<option value="green">зелёный</option>
<option value="purple">фиолетовый</option>
</select>
</div>
</div>
</div>
When user selects for example 'red' in (select) inside (div class='q4EN') in all remaining selects in this (div class=q4EN) selected 'red' option become nonSelectable
(nonSelectable is class in css with display:none)
When user change decision and select 'green' instead of 'red' in first (select) red became available in rest selects and green become nonSelectable
When all 3 select have their value user can't change anything
My js for this is not working and I out of ideas
$(".q4EN").find("select").change(function () {
$(".q4EN").find("select")
.not(this)
.find("option:selected")
.addClass("nonSelectable");
});
I believe the problem is order of operations.
$(".q4EN").find("select").change(function () {
$(".q4EN").find("select") //Finds all select lists
.not(this) //Finds all except the one just changed
.find("option:selected") //Finds selected of all except the one just changed
.addClass("nonSelectable"); //Wont do anything because nothing was selected
});
Try the following:
$(".q4EN").find("select").change(function() {UpdateOptions();});
function UpdateOptions(){
var ss = $(".q4EN").find("select");
ss.find('option').prop("disabled", false); //Enable all before disabling selected
ss.each(function () {
var s = $(this).val();
if(s != undefined && s != "") {
ss.find("option[value=" + s + "]").prop("disabled", true);
}
});
}
This is an alternate way to achieve what you need. It basically iterate through every select element and find the corresponding option and disables it.
$('select').find("option").addClass("selectable");
$('select').on("change",function()
{
// $(this).find("option").prop("disabled",false); // uncomment this if you wish to reset the disabled selection
var $thisId = this.id;
var $selectedOption = $(this).find("option:selected").val();
$('select').each(function()
{
if(this.id !== $thisId)
{
// $(this).find("option").removeClass("non-selectable").addClass("selectable"); // uncomment this if you wish to reset the disabled selection
$(this).find("option[value=" + $selectedOption + "]").prop("disabled",true).addClass("non-selectable").removeClass("selectable");
}
});
})
https://fiddle.jshell.net/a2n234eq/4/
To target a specific group ( like EN and RU ) , change $('select') to $('.q4EN select')

HTML and JavaScript to show text on select change

Ok, so, I got a list, and I want to show some text when the user SELECTS an option from the dropdown, I figured how to make for it for one option but for some reason doesn't work in groups so well.
<select id="mySelect" name="values">
<option value=0>0</option>
<option value=1>1</option>
<option value=2>2</option>
</select>
I intend to "print" some determined text as each option is selected, thanks!
(sorry for any grammar errors, spanish speaker here)
Managed to do it:
<script type="text/javascript">
function showstuff(element){
document.getElementById("una").style.display = element=="una"?"block":"none";
document.getElementById("dos").style.display = element=="dos"?"block":"none";
document.getElementById("tres").style.display = element=="tres"?"block":"none";
document.getElementById("cuatro").style.display = element=="cuatro"?"block":"none";
document.getElementById("cinco").style.display = element=="cinco"?"block":"none";
document.getElementById("seis").style.display = element=="seis"?"block":"none";
document.getElementById("siete").style.display = element=="siete"?"block":"none";
document.getElementById("ocho").style.display = element=="ocho"?"block":"none";
document.getElementById("nueve").style.display = element=="nueve"?"block":"none";
document.getElementById("diez").style.display = element=="diez"?"block":"none";
}
</script>
<select name="type" onchange="showstuff(this.value);">
<option value="una" selected>1</option>
<option value="dos">2</option>
<option value="tres">3</option>
<option value="cuatro">4</option>
<option value="cinco">5</option>
<option value="seis">6</option>
<option value="siete">7</option>
<option value="ocho">8</option>
<option value="nueve">9</option>
<option value="diez">10</option>
</select>
<div id="uno" style="display:none;">uno</div>
<div id="dos" style="display:none;">dos</div>
<div id="tres" style="display:none;">tres</div>
<div id="cuatro" style="display:none;">cuatro</div>
<div id="cinco" style="display:none;">cinco</div>
<div id="seis" style="display:none;">seis</div>
<div id="siete" style="display:none;">siete</div>
<div id="ocho" style="display:none;">ocho</div>
<div id="nueve" style="display:none;">nueve</div>
<div id="diez" style="display:none;">diez</div>
Thanks every one :)

Can't get jquery to set value of select input when value of first select changes

I'm trying to update the value of a select input when I change the value of another select input. I cannot get anything to happen on the page and want to make sure I don't have a syntax error or some other dumb thing in this code.
<div class="group">
<div class="inputs types">
<strong style="font-size:13px;">Category:</strong>
<select name="id" id="ctlJob">
<option value="1">Automotive</option>
<option value="2">Business 2 Business</option>
<option value="3">Computers</option>
<option value="4">Education</option>
<option value="5">Entertainment & The Arts</option>
<option value="6">Food & Dining</option>
<option value="7">Government & Community</option>
<option value="8">Health & Beauty</option>
<option value="9">Home & Garden</option>
<option value="10">Legal & Financial Services</option>
<option value="11">Professional Services</option>
<option value="12">Real Estate</option>
<option value="13">Recreation & Sports</option>
<option value="14">Retail Shopping</option>
<option value="15">Travel & Lodging</option>
</select>
<select name="type" id="ctlPerson"></select>
</div>
</div>
<script>
$(function() {
$("#ctlJob").change(function() {
//Get the current value of the select
var val = $(this).val();
$('#ctlPerson').html('<option value="123">ascd</option>');
});
});
</script>
Try using append instead:
$(function() {
$("#ctlJob").change(function() {
//Get the current value of the select
var val = $(this).val();
var ctl = $('#ctlPerson').append('<option value="123">'+val+'</option>')[0].options;
ctl.selectedIndex = ctl.length-1;
});
});
http://jsfiddle.net/J69q8/
I think you also need to set the 'selected' property. Add
$('#ctlPerson option[value="123"]').attr('selected', 'selected');
to the end of the script. You are currently adding the option to the select list, but are not changing the select list to show it.
<div id="test">
<select name="sel" id="sel">
<option name="1" id="1" value="1">Automotive</option>
<option name="2" id="1 value="2">Business 2 Business</option>
<option name="3" id="1 value="3">Computers</option>
</select>
<select name="sel2" id="sel2"></select>
</div>
<script>
$("#sel").change(function() {
var val = $(this).val();
$('#sel2').html('<option value="1">NEW</option>');
)};
</script>
this works fine for what you need to do.
it's something like what you have

Categories

Resources