JQuery - Require a Form Field if Visible - javascript

I'm using the Jquery code below for a webform in Power App Portals. The code hides the BIL Category drop-down field based on the choice selected in the Type of Funding Request drop-down field. This all works perfectly.
Now I simply want to require the BIL Category field if it is visible. How can I modify the code to achieve this?
$(document).ready(function () {
$("#cr92d_typeoffundingrequest").change(onDisplaySectionChange);
onDisplaySectionChange();
});
function onDisplaySectionChange() {
var varFunding = $('#cr92d_typeoffundingrequest').find("option:selected").text();
if (varFunding === "DWSRF")
{
$('#cr92d_bilcategory').parent().parent().hide();
}
else{$('#cr92d_bilcategory').parent().parent().show();
}
}

else
{
$('#cr92d_bilcategory').parent().parent().show();
$('#cr92d_bilcategory').prop('required', true);
$('#cr92d_bilcategory').closest(".control").prev().addClass("required");
}
Try this.

Related

How change the css with the checkbox with different names?

Currently, I have multiple checkbox inputs with different names(checkit, checktype, checklog) assigned to the inputs.
What I want to do is to have each checkbox to change the color of the background when checked.
However, I dont know how I can assign each one of the checkbox to do some tasks without duplicating the following code ?If possible some examples or tips will be great! I would love to hear from you .
Should I remove name="checkit" if I want to make all the inputs do the same thing? What if I want them to do some slightly different things?
$('input[name="checkit"]').change(function () {
if ($(this).prop('checked')) {
$(this).parent().parent().addClass('alterBackground');
} else {
$(this).parent().parent().removeClass('alterBackground');
}
});
Add the following by , or give some class name to it
$('input[name="checkit"], input[name="checktype"], input[name="checklog"]').change(function () {
if ($(this).prop('checked')) {
$(this).parent().parent().addClass('alterBackground');
} else {
$(this).parent().parent().removeClass('alterBackground');
}
});
Don't use the name atrribute in jQuery and add a common class to each checkbox for a common functionality and access it with class selector in jQuery as shown below.
If you want to do something different with different checkboxes apart from this, then you can add more jQuery code for that specific input tag. It will not affect this code.
$('input.someClass').change(function () {
if ($(this).prop('checked')) {
$(this).parent().parent().addClass('alterBackground');
} else {
$(this).parent().parent().removeClass('alterBackground');
}
});
You can remove the name part from the selector and add selector for input[type='radio']. And if you want to add a bit different logic (I think you mean different classes), you can get the name of the current checked checkbox and use it to make your logic. Something like this
$('input[type="radio"]').change(function () {
var checkboxName = $(this).prop('name');
// if(checkboxName === .....)
});
Updated according to the comment
$('input[name="checkit"], input[name="checktype"], input[name="checklog"]').change(function () {
var checkboxName = $(this).prop('name');
// .............
});
$('input[type="checkbox"]').change(function () {
if ($(this).prop('checked')) {
$(this).parent().parent().addClass('alterBackground');
} else {
$(this).parent().parent().removeClass('alterBackground');
}
});
Use
$('input[type="checkbox"]')
instead of
$('input[name="checkit"]')

Woocommerce radio button check on jquery

I have a custom shipping method on WooCommerce that should show a little box with information to user, only when that custom shipping method is chosen.
So far I have tried with this little code:
jQuery(document).ready(function($) {
$(document).on("change", "#shipping_method_0_my_shipping_method", function() {
if( $("#shipping_method_0_my_shipping_method").prop('checked') == true ){
$('.custom-box').show();
} else {
$('.custom-box').hide();
}
});
});
And it shows the .custom-box just fine when that custom shipping method is chosen. But it does not hide it for some reason, when I choose some other shipping method?
Also it does not show the box if my custom shipping method is chosen on page load, but if I click on some other shipping method, and then my custom shipping method, it will show the box fine.
Working now perfectly. Thanks for the working solution for Томица Кораћ at below!
Try this script:
jQuery(document).ready(function($) {
// Create reusable function to show or hide .custom-box
function toggleCustomBox() {
// Get id of selected input
var selectedMethod = $('input:checked', '#shipping_method').attr('id');
// Toggle .custom-box depending on the selected input's id
// Replace 'shipping_method_0_my_shipping_method' with your desired id
if (selectedMethod === 'shipping_method_0_my_shipping_method') {
$('.custom-box').show();
} else {
$('.custom-box').hide();
};
};
// Fire our function on page load
$(document).ready(toggleCustomBox);
// Fire our function on radio button change
$('#shipping_method input:radio').change(toggleCustomBox);
});
Let me know if this works for you.
Edit
I've updated the script. Please try this and let me know if it works:
jQuery(document).ready(function($) {
// Create reusable function to show or hide .custom-box
function toggleCustomBox() {
// Get id of selected input
var selectedMethod = $('input:checked', '#shipping_method').attr('id');
// Toggle .custom-box depending on the selected input's id
// Replace 'shipping_method_0_my_shipping_method' with your desired id
if (selectedMethod === 'shipping_method_0_my_shipping_method') {
$('.custom-box').show();
} else {
$('.custom-box').hide();
};
};
// Fire our function on page load
$(document).ready(toggleCustomBox);
// Fire our function on radio button change
$(document).on('change', '#shipping_method input:radio', toggleCustomBox);
});

Clear Input field on show / hide | jQuery

I'm wanting to clear the Home Number field when a user clicks on another option within the options list.
Currently if you were to select Option 3, home number displays - this is good. If you are to select Option 1 whilst Option 3 is displayed mobile number shows. This is good too however, my validation bombs because Home Number is required and expects it to be filled out even though the input isn't shown on the page.
My thoughts to get around this would be to clear the Home Number input if another selection has been made? This could be wrong but I'm unsure of another solution to get around this.
DEMO
Here is my jQuery:
var form = {
init: function () {
form.selection();
},
selection: function () {
var homeNumber = $('.home-number'),
mobileNumber = $('.mobile-number');
$("li.checkbox").on('click', function (e) {
//e.preventDefault();
if ($(this).find("input:checkbox").is(":checked")) $(this).addClass("active");
else $(this).removeClass("active");
//If all three are selected show mobile, hide home
if ($("ul li.checkbox.active").length > 1) {
mobileNumber.css('display', 'block');
homeNumber.val('');
homeNumber.hide();
} else if ($("li.last-option").hasClass("active")) {
//If select last option only show home
homeNumber.toggleClass('home-active');
homeNumber.show();
mobileNumber.hide();
//If select last option only hide mobile
mobileNumber.toggleClass('mobile-inactive');
} else {
homeNumber.val('');
homeNumber.hide();
mobileNumber.show();
}
});
}
}
$(function () {
form.init();
});

Need help to display different fields with the help of bullets in HTML form

I am trying to mimic the HTML form present on this link: http://maati.tv/uploads/
I am making it to put on a company's Facebook page.
Now, the issue I am running into is that I am unable to display different fields depending upon the select radio button. I know it requires JavaScript. I've got the related script from this link: https://stackoverflow.com/a/14127709/1315163
$(".radioSelect").each(function () {
showSpecificFields(this);
});
$(".radioSelect").click(function () {
showSpecificFields(this);
});
function showSpecificFields(obj) {
if ($(obj).is(":checked")) {
var radioVal = $(obj).val();
$(".fieldsSpecific").each(function () {
if ($(this).attr('id') == radioVal) {
$(this).show();
} else {
$(this).hide();
}
});
}
}
Can anyone please help me with it? Rest of the code is in this jsFiddle: http://jsfiddle.net/DDJNc/2/
Thanks.
Your answer:
$(".radioSelect").each(function(){
showSpecificFields(this);
});
$(".radioSelect").click(function(){
showSpecificFields(this);
});
function showSpecificFields(obj){
if($(obj).is(":checked")){
var radioVal = $(obj).val();
$(".fieldsSpecific").not('.'+radioVal).each(function(){
$(this).hide();
});
$(".fieldsSpecific."+radioVal).each(function(){
$(this).show();
});
}
}
Check the HTML also
http://jsfiddle.net/DDJNc/4/
. (dot) css selector selects elements containing a given class. That class SHOULD BE present on your HTML for it to work!
Don't use the id attribute for that because you are trying to match a set of elements that belong to a class and not a single element.

Move options between two multi-select fields

I'm trying to do something like this answer: https://stackoverflow.com/a/6256910/1641189
However I want to do it with multi-select fields since they provide scroll bars.
Instead of having each item move as soon as it's clicked, like the answer link above, I am going to have two buttons between the two select fields.
Something like this: http://www.kj4ohh.com/stuff/selectfields.png
How would I do this in javascript/jquery?
How would I return the contents of both select fields back to my flask application?
EDIT:
I had tried the following javascript:
function assign(form)
{
for(i=0;i<form.unassigned.length;i++) {
if (form.unassigned.options[i].selected == true) {
form.assigned.add(form.unassigned.options[i]);
form.unassigned.remove(i);
}
}
//unselect all items
form.assigned.selectedIndex = -1
}
function unassign(form)
{
for(i=0;i<form.assigned.length;i++) {
if (form.assigned.options[i].selected == true) {
form.unassigned.add(form.assigned.options[i]);
form.assigned.remove(i);
}
}
//unselect all items
form.unassigned.selectedIndex = -1
}
but with strange results:
With this script, if you select an item from either select field and hit the appropriate assign/unassign button it works corectly.
If you select two items, only one is moved.
If you select more than two, one is moved, one stays put and the rest vanish.
However if I add an alert() line outputting the current selection being observed, it will produce an alert box for each item selected correctly.
You have to use jquery plugin for better result
http://loudev.com/
You may try this
​$(function(){
$('#toSel1, #toSel2').on('click', function(){
if($(this).attr('id')=='toSel2')
{
var l=$('#sel1 option:selected').length;
if(!l) {
alert("Option not selected !");
return false;
}
$('#sel1 option:selected').each(function(){
$('#sel2').append($(this));
});
}
else
{
var l=$('#sel2 option:selected').length;
if(!l) {
alert("Option not selected !");
return false;
}
$('#sel2 option:selected').each(function(){
$('#sel1').append($(this));
});
}
});
});​
DEMO.

Categories

Resources