I've searched and searched but my jquery/js skills are so poor I can't get it to work.
What I desire is for each option in the selection a different is displayed for further information.
<label class="checklabel">Is it possible to recieve mail?</label>
<select class="mailoption">
<option value="1">Yes</option>
<option value="2">No</option>
</select>
<div class="yesmail">Yes can recieve mail.</div>
<div class="nomail">No can't recieve mail.</div>
When the value is 1 it should display 'yesmail' and option 2 should display 'nomail'. I've tried several lines of coding gathered from stackoverflow but I all had to adjust it which I can't do to this degree.
Can anyone help me out here?
Thanks,
I guess you have had problems with selecting the DOM elements. As #Teemu points out you might want to use ids instead. You can use $(".foobar") to select the elements with the class foobar and $("#foobar") to select the element with id foobar.
$(document).ready(function() {
$(".mailoption").change(function() {
if($(this).val() == 1) {
$(".yesmail").removeClass('hidden');
$(".nomail").addClass('hidden');
} else {
$(".yesmail").addClass('hidden');
$(".nomail").removeClass('hidden');
}
});
});
.hidden {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label class="checklabel">Is it possible to recieve mail?</label>
<select class="mailoption">
<option value="1">Yes</option>
<option value="2">No</option>
</select>
<div class="yesmail">Yes can recieve mail.</div>
<div class="nomail hidden">No can't recieve mail.</div>
Related
I have an inventory form which I have been asked to improve. The user counts product and enters the new count into the form. Then, depending on whether the input is positive or negative, a select box appears with the list of reasons for the discrepancy. The UI is working perfectly, but the reason code will only pass for whichever select element is listed last.
Basically this works to ensure the user is entering negatives correctly so my default is to have the SELECT element for the negative reason second since it will be the one to work. I would appreciate any suggestions for a better approach.
$(document).ready(function () {
if ($("#invAmt").val()=='') {
$("#plus").hide();
$("#minus").hide();
}
//return false;
$(document).on('change', '#invAmt', function() {
if ($("#invAmt").val()>='1') {
$("#plus").show();
$("#minus").hide();
} else {
$("#plus").hide();
$("#minus").show();
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form-group">
<label for="invDec">Enter Amount of Units to Adjust (+/-): </label>
<input name="invAmt" id="invAmt" style="width:50px" >
</div>
<label for="title">Reason for Adjustment:</label>
<div class="plus">
<select class="custom-select custom-select-md" name="reason" id="plus" style="width:370px">
<option value="">--- Select Reason ---</option>
<option value="30">Returned Product</option>
<option value="50">Other (explain in notes)</option></select>
</div>
<div class="minus">
<select class="custom-select custom-select-md" name="reason" id="minus" style="width:370px">
<option value="">--- Select Reason ---</option>
<option value="70">Recount (lost)</option>
<option value="75">Dumped / Died)</option>
<option value="90">Diseased / Pest</option>
<option value="65">Overgrown</option>
<option value="95">Overstock</option>
<option value="98">Loaned Out</option>
<option value="100">Other (explain in notes)</option></select>
</div>
if you want to select multiple options from your drop downs you need to add the "multiple" attribute to your two select statements. Like this:
<select class="custom-select custom-select-md" multiple name="reason" id="plus" style="width:370px">
also, you need to add a submit button.
If you are trying to get just one option for + and one option for - you need to use different names for each of your select boxes. If they are both name='reason' the last one will overwrite the first one. Try:
name='negReason'
name='posReason'
I have a Request with the Chosen Javascript plugin : https://harvesthq.github.io/chosen/
Is there a way to enable a default option value when you're looking for another one?
Let me explain, I have a selector in HTML where I have a lot of different options and the last one is "Other", when it's selectionned, I want to show an input text to let the user add a new choice if he wants.
So I would like to let appear everytime the "Other" option when an user is looking for an ((un)existing) option, but with this plugin I didn't fhind the way to make it.
Let me give you an exemple :
$(".chosen-select").chosen({width: "220px"});
$(".chosen-select").chosen().change(function(){
if($(this).val() == ''){
$('div#newcolor').removeClass('hide');
} else {
$('div#newcolor').addClass('hide');
}
});
.hide{
display:none;
}
<link href="https://harvesthq.github.io/chosen/chosen.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://harvesthq.github.io/chosen/chosen.jquery.js"></script>
<select class="chosen-select" name="test">
<option value="blue">Blue</option>
<option value="red">Red</option>
<option value="yellow">Yellow</option>
<option value="purple">Purple</option>
<option value="">Ohter</option>
</select>
<div id="newcolor" class="hide">
<label>You can add a new color : </label>
<input value="" placeholde="Try with Pink">
</div>
<hr>
<button>Submit</button>
So if you try to look for Pink, you could not select "Other" option and so you can't add one new...
Thanks
I'm working on an HTML view for iAd Producer. I'm trying to add a list of sentences which have alternative words students can choose and to save those values to localStorage as they change and repopulate the selects with those values when the page is revisited.
I'm adapting some code I wrote which works fine to save multiple input boxes on a page. But I have strange behaviour when trying to use it with multiple selects. Basically, no matter which order the answers are completed in, only the last chosen value is being stored. When the page is revisited, the code attempts to put that value in each select. This, of course, fails for the two selects which do not have corresponding values.
I cannot see why this is happening and hope someone can spot the obvious. Thanks.
<div>
<script type="text/javascript">
var uniqueId = "FC2-U2-A-P29";
$(document).ready(function () {
function onStartup() {
$.each( $("select"), function() {
if (localStorage[$(this).attr("value")+uniqueId]) {
$(this).val(localStorage[$(this).attr("value")+uniqueId]);
}
});
}
onStartup();
});
$('.drop').change(function () {
localStorage[$(this).attr("value")+uniqueId] = $(this).val();
});
</script>
<form>
<label class="number">1.</label>
<label class="text">Breakfast is the </label>
<select name="select1" class="drop">
<option value="blank">Choose a word</option>
<option value="one1">one</option>
<option value="first1">first</option>
</select>
<label class="text"> meal of the day.</label>
<br>
<label class="number">2.</label>
<label class="text">I always eat </label>
<select name="select2" class="drop">
<option value="blank">Choose a word</option>
<option value="three2">three</option>
<option value="third2">third</option>
</select>
<label class="text"> meals a day.</label>
<br>
<label class="number">3.</label>
<label name="select3" class="text">My football team is in</label>
<select class="drop">
<option value="blank">Choose a word</option>
<option value="two3">two</option>
<option value="second3">second</option>
</select>
<label class="text"> place in the league.</label>
<br>
<button class="clearButton" onclick="clearAnswers()">Clear </button>
<script type="text/javascript">
function clearAnswers() {
$.each( $("select"), function() {
if (localStorage[$(this).attr("value")+uniqueId]) {
localStorage.removeItem($(this).attr("value")+uniqueId);
$(this).val("blank");
}
location.reload();
});
}
</script>
</form>
</div>
The probable reason for which this code fails in select is because of the following :
$.each( $("select"), function() {
if (localStorage[$(this).attr("value")+uniqueId]) { //select doesn't have any attribute value.
$(this).val(localStorage[$(this).attr("value")+uniqueId]);
}
});
Select tag doesn't have any value attribute. I think .val() is what you need here. If you look at the code, you are basically iterating on select tag and checking value attribute(which doesn't exist) of select. Try changing it to .val() and then try.
To save and load stuff from localstorage use:
localStorage.setItem(<key>, <value>);
and
localStorage.getItem(<key>);
There are multiple problems with your code.
The first one is that you should use .val() instead of .attr('value').
The second one is that the code to add the values in localStorage is executed before the DOM is created fully, so the selects dont exist yet. To overcome this you need to bind the change event on document.ready ( aka $(function() { }); ).
I've made a pastebin with the modified code: http://jsbin.com/jubijuyatu/2/
Your selector is invalid.
Change $(this).attr("value")+uniqueId with $("option:selected",this ).text()+uniqueId; in all occurrences.
Please excuse my complete lack of knowledge I am very new to all this. I have found some code on here that does kind of what I want so am trying to adapt it. Basically I need to show a character on the screen and then change that character using a drop down list
Heres the HTML
<div style = "position: absolute; left: 320; top: 700;" class = "dropdown-menu- portfolio" >
<select id="category-navbar" name="category-navbar" style="width: 131px;">
<option value="1">motif 1</option>
<option value="2">motif 2</option>
<option value="3">motif 3</option>
</select>
And the Java script
<script>
$(".category-navbar value value").click(function() {
// Modifying the label
$('div.dropdown-menu-portfolio label').text($(this).text());
// Removing the class 'select' to the item previously selected
$('option.select').removeClass('select');
// Giving the class 'select' to the item currently selected
$(this).parent().addClass('select');
});
</script>
I have been struggling for a couple of weeks with this on and off and really want to get it cracked. Any help would be massively appreciated!
I'm not 100% sure, but i think this is what you're looking for:
$(document.body).on('change', 'select#category-navbar', function() {
$('.selected-value').text($(this).val());
});
So when you change an option in the dropdown, it will write the selected option value inside the .selected-value div. Demo: http://jsfiddle.net/qp2b9Lof/
You have some errors in your code for example with this:
$(".category-navbar value value").click(function() {
you don't select anything it must be:
$("#category-navbar").change(function() {
Notice the "#" sign, it selects by ID and second you must use change event
Check JSFiddle for full code
http://jsfiddle.net/qm0msyhf/
Try this
<div class = "dropdown-menu-portfolio" ></div>
<select id="category-navbar" name="category-navbar" style="width: 131px;">
<option value="1">motif 1</option>
<option value="2">motif 2</option>
<option value="3">motif 3</option>
</select>
javascript:
$("#category-navbar").change(function(){
$(".dropdown-menu-portfolio").html($(this).val());
});
click here to see demo - http://jsfiddle.net/1wrt94fc/
First off sorry for a re-post, I voted to delete my old post because I'm asking for help on the code now, not just which way is the better route. Any my code has changed several times
On my page there is a drop down to select a country, dynamically loaded from a db. Once the user selects a country two things can happen. 1) If they select Canada or the US a second drop-down appears and the user can select a region. 2) If the user selects any other country it creates an input box so that the user can type the region instead. This all works fine.
Now there is a third input which takes the province/state value so it can be posted. There are only two of us who will use this form so I'm not worried about JavaScript being turned off in the browser.
My issue is that when the user first selects the Canada/US and a region, nothing is filled into the third input unless they change the country selection. However, if they select a country other than Canada/US and have to type the region, it works as expected.
Here is an example of the issue: http://jsfiddle.net/owalsh/BQXZA/3/
If anyone can tell me why I'd appreciate it, thanks
Working here: http://jsfiddle.net/5A4v4/11/
HTML:
<form id="customer_bill_add_post" name="customer_bill_add_post">
<select id="country" name="country">
<option value="0">Select a country</option>
<option value="CA">Canada</option>
<option value="US">United States</option>
<option value="OT">Other</option>
</select>
<select id="province_select" name="province_select">
<option value="0">Select a Province</option>
<option value="AB">Alberta</option>
<option value="AL">Alabama</option>
</select>
<input type="text" id="province_input" name="province_input">
<input type="text" id="province" name="province" />
</form>
Jquery: code (there was some extra change event binding going on) you can prettify it.
$(function(){
//initially hide the textbox
$("#province_input").hide();
$("#province_select").hide();
$('#country').change(function() {
if($(this).find('option:selected').val() == "CA"){
$("#province_select").show();
$("#province_input").hide();
} else if($(this).find('option:selected').val() == "US"){
$("#province_select").show();
$("#province_input").hide();
} else {
$("#province_input").show();
$("#province_select").hide();
}
});
$('#country, #province_select, #province_input').bind("change", function() {
if($('#country').find('option:selected').val() == "CA"){
document.customer_bill_add_post.province.value = document.customer_bill_add_post.province_select.value;
} else if($('#country').find('option:selected').val() == "US"){
document.customer_bill_add_post.province.value = document.customer_bill_add_post.province_select.value;
} else {
//alert('foo');
document.customer_bill_add_post.province.value = document.customer_bill_add_post.province_input.value;
}
});
});
Cheers,