count checkboxes in a form using javascript - javascript

I have quite a lot of check boxes on one form. The check boxes are in different sections of the form. I would like to count the number of checkboxes at the end of each section on my form.
For example I have 6 sections within my form and I have between 6 and 10 checkboxes within each section. I would like to have a textbox with a number value at the end of each section telling me how many check boxes were check within that particular section.
Does anyone have a script for that? I have a snippet from support staff but they don't have a full solution and I don't know JavaScript well enough to finish it. I'm through trying to figure it out so i can finish it. Here is the snippet they sent me:
<script type="text/JavaScript">
function countcheck(checkName){
inputElems = document.getElementsByName(checkName);
count = 0;
for (i = 0; i < inputElems.length; i++) {
if (inputElems.checked === true) {
count++;
document.getElementById("teval_engage7").value = count;
}
}
}
</script>
The script will only count checked checkboxes within that group only. Basically you will need a function for each of your checkbox so that you can have separated counters. This will also require an attribute to your checkbox according to the function in question:
onclick="countcheck(this.name);"

var cb_counts = {};
var inputs = document.getElementsByTagName('input');
for (var i = 0; i < inputs.length; i++) {
var input = inputs[i];
if (input.type = 'checkbox' && input.checked) {
if (cb_counts[input.name]) {
cb_counts[input.name]++;
} else {
(cb_counts[input.name] = 1);
}
}
}
Now the object cb_counts contains properties with the name of each group of checkboxes, and the values are the counts. Do with this what you wish.

Thanks for the quick reply. I use a application call rsform which helps to make forms. On the script I have "teval_engage7" is the text box which stores the value of the number of checkboxes that have been checked. "onclick="countcheck(this.name);"" is the trigger I place under each checkbox question. So when I go to the form and click on a checkbox with that trigger attached to it, the value of "1" shows up in the teval_engage7 text box. The next check box I click on then shows "2" in the teval_engage7 text box. My question is, can you te me using this script you wrote where the values are stored so I can substitute that name for my textbox name. Also, do I use my same trigger "onclick="countcheck(this.name);"" to attach to my checkbox attibute area to trigger the count?
Thanks

Related

Click all checkboxes on a webpage with HTML script (quickbooks/Safar)

So I created the following script to select all check boxes on a page
(function(d) {
var input = d.querySelectorAll('input[type="checkbox"]');
var i = input.length;
while (i--) {
input[i].checked = true;
}
})(this.document);
It does work to do that, however when trying it in Quickbooks while it does select all the boxes, the website does not register it as actually being selected (the total cost at the bottom remains the same, its like it superficially checks the boxes, visually only with no actual register). Any help would be great.
EDIT: Maybe simulating a click instead of changing the box values?
The only thing that changes when physically selecting a box is the value posted below changes to true from false
You should do :
input[i].setAttribute("checked", "");
The checked attribute is a boolean attribute, so the standard way to add it to an element is to pass an empty string for value.
https://developer.mozilla.org/fr/docs/Web/API/Element/setAttribute#Exemple

Jquery dynamic validations html

I'm working in javascript/jQuery forms filling and validation. However I have a following scenario that I want using for-loops.
I've 3 fields in one container, and I have total 4 containers, The first box is must, scha that the first item input text "A" should be validated. And second is radio button, having their respective input fields, so the check is must that which radio button is pressed and made that field validated and the other is not. Let the radio button is Option B and their respective input text boxes are C and D.
Now The rest of there have the same case when there activator check box is checked otherwise no validation. Let my checkBoxes are X. So my code is:
My Code is 70% working, don't know why. Also, I want to check it back, that if the checkboxes aren't clicked(such that unchecked after clicked) it removes all the validations for the upcoming containers.
I've my custom javascript function of validating check boxes names checkRadioand makeRequire/makeUnrequire that accepts an array of input and make all of them as said.
How can I implement the optimal solution for such scenario.
for (var i = 1; i <=3; i++) {
if (checkRadio("X-"+i))
{
for (var j = 2; j <=4; j++) {
for (var k = 1; k <=3; k++) {
if (checkRadio("B"+j+"-1")) {
makeRequired(["C-"+k]);
makeUnRequired(["D-"+k]);
} else if (checkRadio("B"+j+"-2")) {
makeUnRequired(["C-"+k]);
makeRequired(["D-"+k]);
}
};
};
}
My Id's conventions are, A-1, A-2, A-3 and A-4 for first text field.
B1-1 and B1-2 for respective text fields C-1 and D-1 for first container.

Jquery script - Fill select with input text and vice versa

I have 2 scripts that I need to combine, I basically have a script that allows a text input to be filled when you select an item in a select list. I also have a script that fills a select list based on input of a text input. Now I am trying to figure out how can I combine these 2 scripts so they can work together.
Heres an example of a multi select filling a text input.
http://jsfiddle.net/akhyp/1963/
function loopSelected()
{
var txtSelectedValuesObj = document.getElementById('hidden_input');
var selectedArray = new Array();
var selObj = document.getElementById('selected_items');
var i;
var count = 0;
for (i=0; i<selObj.options.length; i++) {
if (selObj.options[i].selected) {
selectedArray[count] = selObj.options[i].value;
count++;
}
}
txtSelectedValuesObj.value = selectedArray;
}
Heres an example of a text input filling a multi select based on input.
http://jsfiddle.net/akhyp/1964/
$("#txt").change(function(e){
var ar = $(this).val().split(",");
$("#sel option").each(function(){
if(ar.indexOf($(this).val()) != -1)
$(this).attr("selected","selected");
});
});
What I am trying to accomplish is something like this:
If text input is null then fill text input with selected option from the select list.
If the text input is already filled then fill in the multi select based on input from the text input.
Any help would be appreciated.
Final update:
Finally got the results I wanted, here the final product http://jsfiddle.net/akhyp/1966/ been working on this for weeks possibly months lol. Really happy to have this working.
I added another change script function, seems to have made it work. Your change set the selected, and mine transfers the values.
$('#sel').change(function(){
$('#txt').val($('#sel').val());
});
http://jsfiddle.net/snlacks/akhyp/1965/

Is there an "Idiot proof" way to re-enable all fields (regardless of there state) on a form?

My problem is that I have javascript that enables/disables some form fields that I have disabled by default that the user doesn't need to send to my business, but can enable them if they need to with a checkbox. However, I obviously have a form provider that is quite finnicky about disabled fields. Basically, the visitor will select checkboxes that will enable certain fields that they need to enter, or choose to.
What I'm curious about, can I create a checkbox that will undo all the fields enabled or disabled state to just enabled, regardless of it's current state, so that all fields get submitted?
Another reason I need this kind of function is because the client receives an autoresponce e-mail, and when the fields are disabled they see %FeildName% if the field was disabled before submitting the form. simply because the field never existed when the data was submitted on the form, obviously because of it's disabled state.
If something can be done, can it be entered in my existing javascript code? I already have a Form Validation script, that I used with javascript.
I'm not exactly great at javascript, and never really even played with jQuery before, nor do I have the intellect to follow jQuery, so any help would be appreciated.
My code can be seen on this page, just view the source.
http://www.gbjimaging.net/order/form/complete/audio.htm
The checkbox at the very bottom of the form, before the submit and reset buttons I'd like to perform this function, so that all information is garenteed to be submitted. Again, thanks for your anticipated help, and it really will be appreciated.
The following example is the way to make sure that all fields of your current form were re-enabled. All of the elements with input, select, textarea and checkbox tags will become enabled as you click a button:
$(document).ready(function() {
$('#switcher').click(function() {
$('input, select').each(function() {
if ($(this).attr('disabled')) {
$(this).removeAttr('disabled');
}
});
});
});
Online example
You need to use jQuery and add it to your page by pasting the following to your page's head:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
In this example Google CDN is being used.
Please read more about jQuery Install and jQuery Usage.
In jQuery:
$(":input").prop('disabled', false);
:input is a jQuery pseudo-selector that matches all types of form input fields. The .prop method gets or sets properties, so this sets all the disabled properties to false.
Something along the lines of :
function validate(OrderAudio) {
if(valid) {
var e = document.getElementsByTagName('input'), i;
for(i = 0; i < e.length; ++i) {
e[i].disabled = false;
}
e = document.getElementsByTagName('textarea');
for(i = 0; i < e.length; ++i) {
e[i].disabled = false;
}
}
}
There is a very easy way to do this. All you have to do is put all the field items in an Array, then when the user checks the checkbox, you can make an Array loop and for every item in the Array (for(var i = 0; i < array.length; i++) if(checkBox.checked == true) {array[i].disabled = false;} else {array[i].disabled = true;}). This will disable all items if checkbox on uncheck, and enable on check.
Here is a small diagram:

Jquery Disabling individual buttons within search results that all have the same classes applied

If you take a look at: http://www.thebullionstore.co.uk/_shop/?_cat=9
You will see a list of products each wrapped in a div with the class product_box. Inside product_box there several other divs and a form with an add to cart button. inside the form there is a hidden input field called stock_amount with a value, the value attribute is the amount of each product that is in stock. Iv also added a number base to the stock_amount class name to distinguish each product listing such as stock_amount0, stock_amount1 ans so on.
I want to be able to disable each button for each individual product after a certain amount of clicks. The amount of clicks will be equal to the value of stock_amount so in-effect a user cant add more products to the cart than is available.
Adding to the cart is currently done with Jquery but I don't know jquery well enough to figure out how to loop through each product listing and do what I described above.
Any help would be much appreciated.
$("button.addtocart").click(function(e) {
// fetch <input name='stock_amount'> within the clicked buttons form element
var stock_amount_input = $("input[name='stock_amount']", $(this).parent());
// fetch <input name='_qty'> within the clicked buttons form element
var qty_input = $("input[name='_qty']", $(this).parent());
// maybe you want to do some check here
var new_amount = stock_amount_input.val() - qty_input.val();
// set the new calculated value in the stock_amount input
stock_amount_input.val(new_amount);
// if the new_amount is less than 1 we dissable the clicked button
if(new_amount < 1) {
$(this).attr("disabled", "disabled");
}
});
To loop thorough elements you can use the jQuery.each(). Or write something like this:
var nodes = $("form");
for(var i=0; i<nodes.length; i++) {
var node = nodes[i];
// do something
}
But I don't think you need to do this. You can preform the check and the disabling of the button in the click-event-handler. Furthermore you shouldn't forget to check if the entered amount (e.g. 1000) doesn't exceed the stock amount.

Categories

Resources