Assuming I have a checkbox array which allows multiple selections to be made. I would like to save only the first selection in a variable but still record and save the other selections (in another variable). Something like :
First_Selected = this_selection (x)
Other_Selected = these_selections(1,2,3)
Using the loop below how would I achieve this??
var Selection_one = " ";
var other_Selections = " ";
for (i = 0; i < tbls.length; i++) {
Selection_one = ?? ;
other_Selections += ?? ;
}
with your logic
var Selection_one = " ";
var other_Selections = " ";
for (i = 0; i < tbls.length; i++) {
if(i == 0)
Selection_one = tbls[i];
other_Selections += tbls[i] + ',' ;
}
Use .slice() to select all the elements after the first one, and then use .join() to concatenate all the values.
var selection_one = tbls[0];
var other_selections = tbls.slice(1).join(' ');
Related
I've got several items I'm trying to display within a canvas. I've json my php data to support doing this in javascript.
Now I'm trying to iterate the database names with a for loop so I don't have to write the code for each.
Here's what I've got so far:
for ((var i=2; i<=17; i++) && (var j=3; i<=18; j++)){
if((row.g1c[j]y + row.g1c[j]m != 0) && ((12*row.g1c[j]y + row.g1c[j]m) > (360 + (12*row.O1y + row.O1m)))){
var g1c[i]w = (360-(12*row.g1c[i]y + row.g1c[i]m)-(12*row.O1y + row.O1m));
} else if (row.g1c[j]y +row.g1c[j]m != 0){
var g1c[i]w = ((12*row.g1c[j]y + row.g1c[j]m)-(12*row.g1c[i]y + row.g1c[i]m));
} else {}
var g1c[i]x = ((12*row.g1c[i]y + row.g1c[i]m)-(12*row.O1y + row.O1m));
var lineHeight = 15;
var maxWidth = 2.5*(g1c[i]w);
var x = 80+(2.5*(g1c[i]x))+(maxWidth/2);
}
This isn't working and I'm 99.999% sure it has to do with the i and j syntax, but everything I've looked up and tried hasn't worked.
If someone would tell me where my screw-up is, I'd be eternally grateful.
Thanks in advance!
Decided do go down the PHP route vice Javascript to avoid having to redo my database...
I'm creating a for loop for each database column and row...
for ($j=3; $j<=$count; $j++){
$l = "g1c".$j."m";
$m = "g1c".$j."y";
I can then use php logic as needed for each row of data that needs to be assessed...
if(($row[$l] + $row[$m]) != 0){
.... do something
}
You can probably do this:
for (var i=2; i<=17; i++){
for (var j=3; i<=18; j++){
if((row.g1c[j]y + row.g1c[j]m != 0) && ((12*row.g1c[j]y + row.g1c[j]m) > (360 + (12*row.O1y + row.O1m)))){
var g1c[i]w = (360-(12*row.g1c[i]y + row.g1c[i]m)-(12*row.O1y + row.O1m));
} else if (row.g1c[j]y +row.g1c[j]m != 0){
var g1c[i]w = ((12*row.g1c[j]y + row.g1c[j]m)-(12*row.g1c[i]y + row.g1c[i]m));
} else {}
var g1c[i]x = ((12*row.g1c[i]y + row.g1c[i]m)-(12*row.O1y + row.O1m));
var lineHeight = 15;
var maxWidth = 2.5*(g1c[i]w);
var x = 80+(2.5*(g1c[i]x))+(maxWidth/2);
}
}
function doGetWord(){
var word = F.gword.value;
var wLength = word.length;
for(var i = 0; i < wLength; i++){
document.getElementById("dword").innerHTML += "_ "
}
}
This is a function that will write _ in a div in html, and what I want is to change them if the user types the corresponding input, for example if the first letter is supposed to be "a" then it would change the first _ to "a".
This is what I got so far:
function doGuessWord(){
dummy = F.t.value
if(dummy.length > 1){
dummy = ""
F.t.value = ""
}
for(var x = 0; x < wLength; x++){
if (substr(x, wLength) == dummy ) {
document.getElementById("dword").innerHTML += "_ "
}
else{
document.getElementById("dword").innerHTML += "dummy "
}
}
}
Could you help me out with this one?
Thanks in Advance!!
Something like this?
https://jsfiddle.net/9z66968a/3/
You will have to adapt it a bit. But you should be able to take the parseText function and pass it the params you need to return the text to insert where ever you want
There you go. I believe this is what you wanted. Feel free if you don't understand something
https://jsfiddle.net/vhsf8gpp/2/
var dashArr = [];
var dummyWord = document.getElementById('dummy');
var input = document.querySelector('input');
var counter = 0;
for(let i= 0; i<10;i++)
{
dashArr.push('_');
}
function WriteContent()
{
dummyWord.textContent = dashArr.map(d=>d).join(''); // This gets rid of the ',' inbetween the dashes
}
WriteContent();
//var charArr = [];
document.querySelector('input').addEventListener('keyup',function(){
var inputString = input.value;
dashArr[counter] = inputString.charAt(inputString.length - 1);
WriteContent();
counter++;
})
I used this post for reference.
For some reason when enter input its being returned in reverse. I can get it to work using a for loop, but how would I do this using a while loop?
var input = document.getElementById('userinput').value;
var i = input.length;
while (i--) {
document.getElementById('message').innerHTML += input[i] + "<br/>";
}
Either reverse the loop counter and condition:
var input = document.getElementById('userinput').value;
var i = 0;
while (i < input.length) {
document.getElementById('message').innerHTML += input[i] + "<br/>";
}
Or reverse the order of concatenation inside the loop:
var input = document.getElementById('userinput').value;
var i = input.length;
while (i--) {
document.getElementById('message').innerHTML =
input[i] + "<br/>" + document.getElementById('message').innerHTML;
}
Note that either way you shouldn't update .innerHTML in a loop: as a general principle try to minimise DOM updates, so use a temporary variable to build up the desired string and then assign .innerHTML once after the loop. Or for what you're actually doing here you don't need a loop:
var input = document.getElementById('userinput').value;
document.getElementById('message').innerHTML = input.split("").join("<br>");
Simplest way to achieve your goal:
var input = document.getElementById('userinput').value;
document.getElementById('message').innerHTML += input.split('').join('<br/>')
Here's a different approach using split and forEach
var input = document.querySelector("#userinput").value;
var html = "";
input.split("").forEach(function(char){
html += char + "<br>";
});
document.querySelector("#message").innerHTML = html;
function successCallback(caRecords) {
var x = document.getElementById("custAccount"); // select
var option1 = document.createElement("option"); //options
//var accno = 0;
// caRecords i am fetch from MS CRM
var count = caRecords[0].results.length;
if (caRecords != null && count > 0) {
alert("records are not null");
for (var i = 0 ; i < count; i++)
{
var text = caRecords[0].results[i].new_name;
// alert(text + "J=" + j);
option1.text = text;
option1.value = j;
x.add(option1);
j++;
}
}
I got six records and try to insert that values into select as option. It showing last value of my 6 values.
Can anyone help me to improve my code?
You can iterate your values like this...
function successCallback(caRecords) {
var x = document.getElementById("custAccount"); // select
var options = "";
var count = caRecords[0].results.length;
if (caRecords != null && count > 0) {
alert("records are not null");
for (var i = 0; i < count; i++) {
options += "<option value=" + j + ">" + caRecords[0].results[i].new_name + "</option>";
j++;
}
x.innerHTML = options;
}
I have a table with n number of rows with checkboxes and a what i want to do is if i select a checkbox the value should go to the text area, so i stored all elements in an array first, but it isnt happening, as you can see i added alerts as well to check it out. please help.
window.onload = function () {
var oRows = document.getElementById('rnatable').getElementsByTagName('tr');
var iRowCount = oRows.length;
alert('Your table has ' + iRowCount + ' rows.');
var i = 0;
cb = new Array(iRowCount);
while (i <= iRowCount) {
var id = 'check'+ i;
cb[i] = document.getElementById(id);
i++;
}
//alert('Your table has ' + cb[i].value + ' rows.');
for(var a=0; a < iRowCount; a++) {
var fasta = document.getElementById('fasta');
if(cb[a].checked) {
fasta.value = cb.value + ",";
};
};
}
Are you seeing an error in the console? I suspect that when while (i <= iRowCount) runs when i === iRowCount that document.getElementById(id) isn't yielding a result, and that then when you use that value, bad things happen.
Also, each lap through the fasta loop overwrites the previous value. You probably want something like fasta.value += cb.value + ","; instead.