Check series of checkboxes according to string - javascript

I have group of checkboxes nearly equals to 100 all are with name checkbox1...checkbox1oo. I am getting a string from database in the bit format. Now I need to check checkboxes using for loop according to everyposition for string..
just taking example. String is 10010. that means I have to select checkbox 1st AND 4th. Hows it will be possible.
for (i=1;i<=5;i++)
{
check every position of sting==1
than document.formname.checkbox[i]==checked.
}
I wish to do something like that.

for( var i = 0, l = string.length ; i < l ; i++ ) {
if( string.charAt(i) === "1" ) {
document.forms[formname]['checkbox' + i].checked = true;
}
}
where string is the string from your database, and formname is the id of the form you're manipulating

Get all the tags, loop through, get check boxes, build the string.
var elements = document.getElementsByTagName('input');
var str = "010101";
for(var i = 0; i< elements.length; i++) {
if (elements[i].type == "checkbox") {
var index = +(elements[i].name.match(/\d+/g)[0]); // get index
elements[i].checked = !!(+str.charAt(index -1));
}
}
http://jsfiddle.net/UMuxP/
!!(+str.charAt(index -1))
could easily be:
str.charAt(index -1) === "1"

Related

How to remove the 1st character if a comma from a cell value using GAS?

I'm iterating over a range and the rows whose conditions are met, gets one of its column's cell value pushed into an array. Now, from the second iteration on, there shouldn't be a , at the beginning, but this is naturally inherited.
for (var a = 0; a < dataRng.length; a++) {
if (dataRng[a][1] == true && dataRng[a][0] == 'SHIPPED' && dataRng[a][40] != 'Yes') {
msgBody.push(dataRng[a][37].toString().replace(',', '') + '\n');
var iteratedRow = a + 3
sheet.getRange(iteratedRow, 41).setValue('Yes')
}
}
This is the result now:
So, right at the beginning of the second row, that comma shouldn't be there and it should begin with 'NPA...'
Thank you!
I think this is what you're looking for. There's probably cleaner ways of doing it, but it collects the value, then tests for a comma, then pushes to an array without the comma if one existed.
for (var a = 0; a < dataRng.length; a++) {
if (dataRng[a][1] == true && dataRng[a][0] == 'SHIPPED' && dataRng[a][40] != 'Yes') {
var aValue = dataRng[a][37].toString().replace(',', '') + '\n';
if (aValue.charAt(0)=== ","){
aValue = aValue.slice(1);
}
msgBody.push(aValue);
var iteratedRow = a + 3
sheet.getRange(iteratedRow, 41).setValue('Yes')
}
}

get string matching then store second string and store it

have to search for classname with string matching "Routed:" then get the string of the following classname in this case "W000000" in the image below. there are over 65 of these on the page at any given time i just need to get the first one and store that value.
You could use a for loop for the elements and get the next one.
var tds = document.getElementsByClassName('ms-cellstyle ms-vb2');
var found = false;
var firstValue = '';
for (var i = 0; i < tds.length && !found; i++) {
if (tds[i].innerHTML.indexOf('Routed') >= 0 && i < tds.length - 1) {
firstValue = tds[i + 1].innerHTML;
found = true;
}
}
console.log(firstValue);

regexp in javascript to get all of the fields of an array which start with a specific string

var myArr=["test1.nameA", "test1.nameB", "test2.nameC", "test2.nameA"];
I want to get those strings which start with "test1."
I have tried the following:
1)could you please let me know why I'm still getting all of the strings in the array?
for (var i = 0; i < myArr.length; i++) {
if ((myArr[i]).search(new RegExp("test1") != -1)) {
console.log(myArr[i]);
}
}
but this one is returning all elements! What am I doing wrong?
2) Also is it possible to check that it only starts with "test1" not just contains!
Updated the question:
myArr=["test1.nameA", "test1.nameB", "test2.nameC", "test2.nameA"];
var input="test1";
for (var i = 0; i < myArr.length; i++) {
if(myArr[i].search( new RegExp (/input/)) != -1) {
console.log(myArr[i]);
}
}
Thanks! Please let me know if you need more clarifications
You can keep only the values that match a regexp using Array.prototype.filter like this:
myArr.filter(function (i) { return /^test1\./.test(i); })
And to check that something just beggins with a string use /^string/. Note the ^
You are testing the value of new RegExp("test1")
if ((myArr[i]).search(new RegExp("test1") != -1)) {
^^^^^^^^^^^^^^^^^^^^^^^^^
And new RegExp("test1") will never equal -1, which mean your test is always true.
This is what you need:
var myArr=["test1.nameA", "test1.nameB", "test2.nameC", "test2.nameA"];
for (var i = 0; i < myArr.length; i++) {
if ((myArr[i]).search(new RegExp("test1")) != -1) {
console.log(myArr[i]);
}
}
Or better still, to avoid creating a new RegExp every time around the loop:
var myArr=["test1.nameA", "test1.nameB", "test2.nameC", "test2.nameA"];
var rex = new RegExp("test1");
for (var i = 0; i < myArr.length; i++) {
if ((myArr[i]).search(rex) != -1) {
console.log(myArr[i]);
}
}
Additionally, .search() returns the position that the expression is found in the string, and -1 if it isn't found. So when it returns zero, that means it's found at the start.
You could just test the values against the same RegExp.
var myArr=["test1.nameA", "test1.nameB", "test2.nameC", "test2.nameA"];
var pattern = /^test1\./; // begins with test1 followed by a dot
for(var i = 0; i < myArr.length; i++) {
if(pattern.test(myArr[i])) {
console.log(myArr[i]);
}
}
Working example: http://jsbin.com/qilinohora/1/edit?js,console

Search Box Function Not Eliminating Correct Value

I am trying to make a simple website where the user types input into a search box, and every time a key is press, their input is compared against the first row of a 2 dimensional array which checks for character matches. If the character they input doesn't match anything, I want it to remove that specific bucket of the array. I have attempted to write basic code for this I thought would work, and have it up at the demo site linked. (Sorry I am just using a free host and havn't optimized the equation table at all so bear with it)
http://fakefakebuzz.0fees.net/
As you can see, the function is not eliminating the appropriate table rows. For example, typing "A" should not eliminate the "Average Current Equation" row because the first letter of that is A, which means matches should not = 0.
I have been looking through this code all morning, and cannot find where I went wrong. I also want to stick to vanilla js.
Any help?
Thanks so much.
I just debugged your code, and the function you use is narrowTable. first remove onkeypress from body node
<body onload="printTable()" onkeypress="narrowTable()">
and add onkeyup instead to you input, like this:
<input type="search" name="equationSearch" id="equationSearch"
placeholder="Equation Search" autofocus="" onkeyup="narrowTable()">
because when you use onkeypress the key value hasn't been added to the input box and your input value has no value in your function, which is:
function narrowTable() {
var newTableContent = "";
var matches = 0;
var input = document.getElementById("equationSearch").value;
//input has no value
for (var i = 0; i < tableData.length; i++) {
for (var j = 0; j < tableData[i][0].length; j++) {
if (input == tableData[i][0].charAt(j)) {
matches++;
}
}
if (matches == 0) {
tableData.splice(i, 1);
}
matches = 0;
}
for (var i = 0; i < tableData.length; i++) {
newTableContent += "<tr><td>" + tableData[i][0] + "</td><td>" + tableData[i][1] + "</td></tr>";
}
document.getElementById("table").innerHTML = newTableContent;
}
the other problem your code has is after printing your table, your tableData variable has changed because you have removed some of indexes. you should reset the tableData to its original value or you can do:
function narrowTable() {
//create a copy of your original array and use currenttableData instead
var currenttableData = tableData.slice();
var newTableContent = "";
var matches = 0;
//your code
}
the other problem here is the way you search for your input value:
for (var j = 0; j < tableData[i][0].length; j++) {
if (input == tableData[i][0].charAt(j)) {
matches++;
}
}
if (matches == 0) {
tableData.splice(i, 1);
}
you can easily do this, instead:
if(tableData[i][0].search("input") == -1){
tableData.splice(i, 1);
}
First, to check if a string is a substring of another string, you can use indexOf. It will return -1 if the string is not found in the other string.
Second, you shouldn't alter the array while you are still looping through it, unless you make sure to alter the counter variable (i in this case) appropriately.
var dataToRemove = [],
i;
for (i=0; i<tableData.length; i++) {
if(tableData[i][0].indexOf(input) == -1) {
// add the index to the to-be-removed array
dataToRemove.push(i);
}
// remove them in reverse order, so the indices don't get shifted as the array gets smaller
for(i = dataToRemove.length - 1; i >= 0; i--) {
tableData.splice(i, 1);
}
dataToRemove = [];
for (i=0; i<tableData.length; i++) {
newTableContent += "<tr><td>" + tableData[i][0] + "</td><td>" + tableData[i][1] + "</td></tr>";
}
I haven't tested this code, but it should at least give you a better idea of how to make this work.

Retrieve all DOM elements in javascript

My application needs to retrieve all DOM elements in javascript and
and put there values into one variable
i.e
if my application is having three text boxes then i want there values in comma separated form in javascript variable
If you want all DOM elements, which you probably don't, but if you do...
document.getElementsByTagName('*');
What I think you want is something like this
var form = document.getElementById('my-form').
var inputs = form.getElementsByTagName('input');
var inputsCsv = [];
for (var i = 0, length = inputs.length; i < length; i++) {
if (inputs[i].type === 'text') {
inputsCsv.push(inputs[i].value);
}
}
inputsCsv = inputsCsv.join(',');
in below example i am taking values of all textboxes of a table. you can pass form name there.
var frm = document.getElementById('tblemail').getElementsByTagName("input");
var len = frm.length;
var myval='';
for (i=0;i< len;i++)
{
if (frm[i].type == "text")
{
if(myval =='')
{
myval = frm[i].value;
}else{
myval += ',' + frm[i].value;
}
}
}

Categories

Resources