I created two input boxes to store some value. I have alotted a value for each inputs such that on entering those values only i should get the current button changed to a different one. Now I have two questions here-
On implementing this , I am not able to see the by default button which should be present here, instead I see the changed button which i wanted that too only when I enter the values i alotted in the input boxes. How can i fix this such that if i dont enter anything or enter wrong values in input boxes my default button should still be visible.
I want these two input boxes under a particular option lets say "XYZ" . This means when i choose "XYZ" then only these boxes should appear and work after providing values.
I am trying to use a dropdown here but not able to get the input boxes after clicking the dropdown option. They are side b y side the dropdown.
I am attaching some code to explain, cant attach whole code because of company policies.
XYZ:
<select id="xyz" name="xyz" onchange="renderxyzButton()">
<option Selected Disabled>Select</option>
<option value="XYZ">XYZ</option>
</select>
X:
<input type="text" size="50" id="x" name="x" />
Y:
<input type="text" size="50" id="y" name="y" />
document.querySelector("#xyz").addEventListener('change', function(e) {
e.somefunc();
if ($("#xyz").val() === "XYZ") {
renderxyzButton();
} else {
renderButton();
}
});
function renderxyzButton() {
if ($("#x").val() === "P") {
document.querySelector("#x").addEventListener('input', function(e) {
e.somefunc();
renderButton();
});
} else {
renderButton();
}
if ($("#y").val() === "C") {
document.querySelector("#y").addEventListener('input', function(e) {
e.somefunc();
renderButton();
});
} else {
renderButton();
}
}
Related
I have a form which is saved to Local Storage as a String. My form however has few checkboxes, radio buttons and so on that use attribute instead of value. How can I perform such a check and pass on right "values" to the Local Storage in my dataSave function?
function dataSave(){
var mngrFields = {};
$(".mngr-field").each(function(){
if ($(".mngr-field").is("checkbox")){
mngrFields[this.id] = this.attr("checked");
} else {
// Default method to get input-text values
mngrFields[this.id] = this.value;
}
});
localStorage.setItem('fieldString', JSON.stringify(mngrFields));
}
The default method to get input-text values is working just fine on it's own, but when the checkbox checker is included, it breaks the whole application because of an error.
Any ideas on how can I build up a good checker that I might be able to use for radio buttons as well?
In general all you need to do is just to check the type of input fields and to detect checked attribute for radio and checkbox ones - both of them have true/false for it. Here is the example with all basic HTML types:
<input class="mngr-field" type="radio" id="field-01" checked> Checked radio<br>
<input class="mngr-field" type="radio" id="field-02"> Unchecked radio<br>
<input class="mngr-field" type="checkbox" id="field-03" checked> Checked checkbox<br>
<input class="mngr-field" type="text" id="field-04"> Comment on how it went?<br>
<select class="mngr-field" id="field-05">
<option value="">Rate</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select> Rate it<br>
<textarea class="mngr-field" id="field-06">Area text</textarea><br>
<input type="button" value="Save to LS" onclick="dataSave()">
<script>
function dataSave() {
var mngrFields = {};
$('.mngr-field').each(function () {
if(this.type == 'radio' || this.type =='checkbox')
mngrFields[this.id] = this.checked;
else
mngrFields[this.id] = this.value;
});
localStorage.setItem('fieldString', JSON.stringify(mngrFields));
console.log(JSON.parse(localStorage.getItem('fieldString')));
}
</script>
Console log output:
Object {field-01: true, field-02: false, field-03: true, field-04: "Input text", field-05: "2", "field-06":"Area text"}
field-01: true
field-02: false
field-03: true
field-04: "Input text"
field-05: "2"
field-06: "Area text"
I'm sure you know how to proceed with localStorage string after that. :)
It seems this line if ($(".mngr-field").is("checkbox")){ is the culprit. jQuery is checks for the conformance of an element with a selector, and "checkbox" does not seem to be a valid selector for a checkbox. Try if ($(".mngr-field").is(":checkbox")){ instead.
I have a select area for the user:
<select class="span12" placeholder="Enquiry Type" name="enquiry" id="enquiry">
<option value='' selected></option>
<option value='cv'>Submit CV</option>
<option value='vacancy'>Submit Vacancy</option>
</select>
I have a hidden field in the form which allows the user to upload their cv. I now need it so that if the user selects submit cv, the field will display itself.
<input type="file" name="cv" accept="application/pdf,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document" style="display:none;">
I know that i would be able to check the user input of the select using an on click function. Is there a way of doing it without that (there and then)? And then if that option is selected, display the hidden file upload.
Try this:
$('#enquiry').on('change', function(e) {
if($(this).val() === "cv") {
$('input[name="cv"]').show();
}
});
This works:
http://jsfiddle.net/dL8evt33/
:)
You can achieve this by hooking to the change event of the select and checking the selected value. You can then use toggle() to hide or show the input as needed. Try this:
$('#enquiry').change(function() {
$('input[type="file"]').toggle($(this).val() == 'cv');
});
Example fiddle
remove display: none; first now do write this code.
$("[id$=fileupload]").hide();
if("[id$=enquiry]").val('cv')
{
$("[id$=fileupload]").show();
}
else
{
$("[id$=fileupload]").hide();
}
On change event you can display the input type file using its id. Give 'file' as id to hidden field -
$('#enquiry').on('change', function() {
if("cv" == this.value){
jQuery("#file").show(); // or jQuery("#file").toggle();
}
});
I'm hoping you can please help.
I have the following HTML code:
<select id='bonus1frequency'>
<option value="0">Annual</option>
<option value="1">Half Yearly</option>
<option value="2">Quarterly</option>
</select>
Depending on what option is selected, fields are displayed in a divider. For example if you select annual, a field to enter annual bonus is displayed. If you select 'Half Yearly' two fields to enter two half yearly bonuses are displayed.
The trouble I'm having is that if you select 'Half yearly' for example, then enter what the half yearly bonus into the fields, then you change your mind and decide you want to enter an annual bonus instead, the values that you enter into the half yearly bonus are retained. I need them to be set to zero when you switch between options in the html select box.
I tried the following code to help with this but it's not working and i'm not sure why. All it does is prevent me from putting anything at all into the half year boxes.
var bonus_val_1 = document.getElementById("bonus1frequency");
var strUser = bonus_val_1.options[bonus_val_1.selectedIndex].value;
if (strUser = "0") //if annual bonus is selected
{
document.getElementById("halfyear_bonus_one").value = 0.00;
}
Edit:
I'm using the following JQUERY in the main HTML document to actually switch what fields are displayed when the user selects each option. So if the user selects option value 0 'annual' in the select box, the following code will show the annual bonus divider containing the annual bonus field, and hide the dividers that show half yearly and quarterly bonus fields.
Based on comments about how I need to tie resetting these fields to the change event, I just thought maybe I can reset the fields to zero within the following jquery code. However I'm even less familiar with JQUERy and not sure how to do that...any help would be much appreciated.
$('#bonus1frequency').on('change', function() {
if ( this.value == '0')
//.....................^.......
{
$("#annual").show();
$("#halfyearly").hide();
$("#quarterly").hide();
}
if ( this.value == '1')
//.....................^.......
{
$("#annual").hide();
$("#halfyearly").show();
$("#quarterly").hide();
}
EDIT 2
I have updated the code as follows:
$('#bonus1frequency').on('change', function() {
if ( this.value == '1')
//.....................^.......
{
$("#annual").show();
$("#halfyearly").hide();
$("#quarterly").hide();
$("#halfyear_bonus_one").val("0");
This prevents me from entering a value into the halfyear bonus one field. It just keeps resetting the value to zero. I've added an empty 'choose' option to the select box but it hasn't made a difference. Any ideas?
Please refer following for help and add code for quarterly. Use jquery instead of using javascript code to get elements.
<select id="bonus1frequency">
<option value="0">Annual</option>
<option value="1">Half Yearly</option>
<option value="2">Quarterly</option>
</select>
<div id="annual" style="display: none;">
<input type="text" id="a1">
</div>
<div id="half" style="display: block;">
<input type="text" class="b2">
<input type="text" class="b2">
<script>
$('#half').hide()//Same as this hide quarterly div
$("#bonus1frequency").change(function(){
var strUser = $(this).val()
if (strUser === "0") //if annual bonus is selected
{
$('#annual').show()
$('#half').hide()
$(".b2").val(0);
}else{
$('#half').show()
$('#annual').hide()
$('#a1').val(0)
}
})
</script>
</div>
The problem with your code is that you're not binding it to the select's change event. It will run when loading the page, and since the default option in the dropdown is "Annual", the value which will appear as the value of #halfyear_bonus_one would be 0.00 and won't change, even if the user will choose another option in the dropdown.
You should create a js function which contain your code (with few corrections)
and then bind it to a change event of the dropdown.
Moreover, you should add conditions to handle the other scenarios: "half yearly" and "quarterly".
var bonusval1 = document.getElementById("bonus1frequency");
bonusval1.onchange = function () {
var strUser = bonusval1.options[bonusval1.selectedIndex].value;
if (strUser == "0"){ //if annual bonus is selected
document.getElementById("halfyear_bonus_one").value = "0.00";
} else if(strUser == 1){ //Half yearly is selcted
document.getElementById("halfyear_bonus_one").value = "1.00";
} else { //quertly is selected
document.getElementById("halfyear_bonus_one").value = "2.00";
}
}
Now, there's another problem - the default value is the first value - but the "onchange" event won't trigger. We can run the function outside of the onchange event in order to handle the default value or you can add a blank option to the dropdown as the first option, something like "Choose:" or whatever.
Jsfiddle
Regarding the second question
I didn't understand why would you use jQuery all of a sudden when you already
wrote pretty good js code. Those kind of decisions should be considered in the beginning of the process.
Anyway, You can use the .val() function to change the value of the input fields. I don't have the code of your form and the dividers but it should be pretty much like the following:
if ( this.value == '0')
//.....................^.......
{
$("#annual").show();
$("#halfyearly").hide();
$("#quarterly").hide();
$("#id_of_input_for_halfyearly").val("0");
$("#id_of_input_for_quarterly").val("0");
}
I think this will help you out.
jQuery('#bonus1frequency').on('change',function(){
if(this.value==1){
jQuery('.yearly').val('').show();
jQuery('.half-yearly').val(0).hide();
jQuery('.quarterly').val(0).hide();
}else if(this.value==2){
jQuery('.yearly').val(0).hide();
jQuery('.half-yearly').val('').show();
jQuery('.quarterly').val(0).hide();
}else if(this.value==3){
jQuery('.yearly').val(0).hide();
jQuery('.half-yearly').val(0).hide();
jQuery('.quarterly').val('').show();
}
})
Check this Demo
I am new to Javascript and I am trying to create a form that will take bookings for a basketball tournament.
I have a radio button list and a drop down menu. The radio buttons allow the user to choose the team they want to play for and the drop down menu lets the user choose which day to play.
However, not all teams can play every day.
For example;
I'm trying to set up the form so that if the user selects that they want to play in Team A (via radio button with name='team'), since Team A plays every day except Sundays, only Sunday is disabled from the drop down menu.
So if the user selects Team A on the radio buttons, then in the select menu, Sunday is disabled from choice.
I'm really not quite sure where to begin.
Thanks!
EDIT: I know there are similar questions but I wasn't able to find one that involved a single value in a select group being changed based on whether or not a radio button has been checked. Since they are different form elements I don't know how to call on each specific one in the script.
EDIT 2 (CODE):
var gameTime = function ()
{
if ($('input:radio[name="team"]').val == 'Team A')
{
$("option[value='Sunday']").prop("disabled", true);
}
};
That's what I had. Didn't seem to be working
Without seeing your code here is a very simple solution
$('input').click(function() {
if($('#team_A').is(':checked'))
{
alert("checked");
$(".sunday").attr('disabled','true');
}
else if($('#team_B').is(':checked'))
{
$(".sunday").removeAttr('disabled');
}
});
Is that What you want ??
New Demo
I don't know how your team schedules are saved, so I just made something up. Basically when you choose a team, an array is referenced to see which days are available for that team, then the appropriate options in the menu are disabled.
HTML:
<p>Choose a team:</p>
<div>
<input type="radio" name="team" id="team-a" value="0" />
<label for="team-a">Total Ballers</label>
</div>
<div>
<input type="radio" name="team" id="team-b" value="1" />
<label for="team-b">Party People</label>
</div>
<div>
<input type="radio" name="team" id="team-c" value="2" />
<label for="team-c">Weekend Warriors</label>
</div>
<p>Choose a day:</p>
<select>
<option>-- Choose a day --</option>
<option>Monday</option>
<option>Tuesday</option>
<option>Wednesday</option>
<option>Thurday</option>
<option>Friday</option>
<option>Saturday</option>
<option>Sunday</option>
</select>
JS (jQuery):
//schedules are saved in a 2D array, the seven numbers correspond to Monday through Sunday
//1 = available, 0 = not available
//first set for team A, second for team B, third for team C
var schedules = [[1,1,1,1,1,1,1], [1,1,1,1,1,0,0], [0,0,0,0,0,1,1]];
$('input').on('click', function() {
//looks at the value of the clicked radio button
var value = $(this).val();
//grabs the corresponding array from schedules
var schedule = schedules[value];
//loops through each day in the menu, disable that option if the array shows a 0 for that day
$('option:not(:first-child)').each(function(i) {
if (!schedule[i]) {
$(this).attr('disabled', true).css('background-color','#eee');
} else {
$(this).attr('disabled', false).css('background-color','white');
}
});
}).on('change', function() {
$('option:first-child').attr('selected', true);
});
//last part changes the menu back to 'choose a day' when a different team is chosen
Here's a fiddle.
Is it possible to select a specified value from a combo box by clicking on a button.
option 1:
Suppose you want to set value to 3 in your dropdown named 'sel'
<input type="button" value="set to 3" onclick="document.getElementById('sel').value = 3" />
option 2:
If you want to set specific text (say 'myvalue') as selected display text (which already exists in the dropdown list), then you can use loop
for (i=0;i<document.getElementById('sel').length;i++)
{
if ('myvalue' == document.getElementById('sel').options(i).text)
{
document.getElementById('sel').options(i).selected = true;
return;
}
}