Get inputs value and separated by "|" - javascript

I have inputs and I need to get values and separated by "|" symbol.
My input:
Output what I need:
00:00|00:00|00:00
My code is :
(and it's not working)
var timesArray = $('table').find('input');
var times = timesArray.val();
$('.alltimes').val(times);

Given that .val returns the value of each element (whatever input type it is in your screenshot), then you can use .map: http://jsfiddle.net/RrCYD/.
var values = $("input").map(function() {
return $(this).val(); // map each element to its value
}).get().join("|"); // get real array and join

See comments in code:
// Create an array to store the input values
var inputValues = [];
// Iterate over input's and store their value in the array
$('table').find('input').each(function () {
inputValues.push(this.value);
});
// Create pipe=delimited string from array of values
var delimitedInputValues = inputValues.join("|");
Additional Information
MDN - array.join
jQuery - .each()

Related

How to generate one object key with an array of stored values from multiple on click events using localstorage and Jquery

I'm new to coding, and I need to display past search values from an input field using localstorage. The only way I can think of is by using one object key with an array of stored values from an on click event. Problem is, I can only get one position to appear as a value, with each value generated replacing the last. I've tried for loops and can't seem to get it to work. This is the code I have so far:
$('.search-city').on('click', function(e){
e.preventDefault();
var textArr = [];
var text = $(".form-control").val();
textArr.push(text);
localStorage.setItem("value1", textArr);
});
$('.search-city').on('click', function(e){
e.preventDefault();
var search = localStorage.getItem("value1")
This would work:
$('.search-city').on('click', function(e){
e.preventDefault();
// get the value from local storage
var localValue = localStorage.getItem('value1');
// if we had a value, parse it back to an array, if we dont, create an empty array
var textArr = localValue ? JSON.parse(localValue) : [];
// get the text from the search input, dont use "form-control"
// you're likely to have several of those on the page
// give the element a custom class like "search-input" and use that (id would be even better)
var text = $('.search-input').val();
// add the text to the array
text = trim(text);
if (text) {
textArr.push(text);
}
// enforce a size limit here by removing the 0 index item if the count has grown too large
var maxAllowed = 10;
while (textArr.length > maxAllowed) {
textArr.shift();
}
// localstorage can only hold simple strings so we'll JSON stringify our object and store that
localValue = JSON.stringify(textArr);
localStorage.setItem("value1", localValue);
});

Issues with indexOf in Javascript

I am having issues with indexOf in Javascript. It seems IndexOf fails to find a string when the it is obtained using .getValues() from a spreadsheet.
For example, the function below works fine.
function Narek() {
var array = ["Armine", "Narek", "Suren"]
var ggg = array.indexOf("Armine");
}
But this function, which gets the same names from the googlesheets returns -1.
function Narek() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("HL rota");
var array = ss.getRange(45,1,3,1).getValues();
var pickedHLM = ss.getRange(49,1,1,1).getValue();
var ggg = array.indexOf(pickedHLM);
Logger.log(pickedHLM)
Logger.log(ggg);
}
Any one can help?
While Range#getValue() returns the top-left cell content (in this case the value inside A49), #getValues() returns a two-dimensional array of the cell contents of the whole range (ie A45:A47). Each inner array holds the row's values.
So perhaps more similar to:
var values = [["Armine"], ["Narek"], ["Suren"]];
This can be transformed into your expectation of a flat array.
var array = values.map(function(row) { return row[0]; });
// ["Armine", "Narek", "Suren"]

adding input value in multidimensional array?

I am facing problem in java-script problem as
I have to collect all subrole from screen whenever user focus out from input field.this is my code
var subrole_id = [];
$("ul :input[class^='sub']").live('focusout', function() {
var get_id = $(this).attr('class').split("_");
var ids = get_id[2];
var ids_index = get_id[3];
subrole_id[ids][ids_index] = this.value;
});
but this is giving error
TypeError: subrole_id[ids] is undefined
subrole_id[ids][ids_index] = this.value;
Actually I want to collect value of subrole input field and add them in array with index ids one by one and before adding that value in array I have to check whether that present value is present in array or not if yes then do not add and give error.
Please suggest.
There isn't a true multidimensional array in javascript, just an array of array, so you have to create the subArray:
subrole_id[ids]=subrole_id[ids]||[];
subrole_id[ids][ids_index] = this.value;

jQuery getting value from dynamic array

I have an array with divs ids (in my case its all divs ID values od parent div (#area) ):
jQuery.fn.getIdArray = function () {
var ret = [];
$('[id]', this).each(function () {
ret.push(this.id);
});
return ret;
};
var array = $("#area").getIdArray();
I need to get an array field value, something like this:
var lef = $("#array".[0]).css("left");
Taking a wild swing at it (see my comment on the question):
var array = $("#area").getIdArray();
var lef=$("#" + array[0]).css("left");
That assumes that getIdArray returns an array of strings, where each string is an id value for a DOM element, and that you want to get the left value for the first of those elements.
So for instance, if the array comes back as:
["foo", "bar", "charlie"]
then the selector created by "#" + array[0] is #foo, so you end up getting the left value for the foo element.
If you have an actual JS array within your variable array just use bracket notation to access each individual ID.
// I have the # before-hand since I'm assuming you have just the ID name
var lef = $('#' + array[0]) // this will access the 1st one in the array
I think you are looking for this :
var divYouWantToChange = $("#"+array[0]);
I try to formulate this as an answer because getIdArray is not a jquery function and we don't know what it does. If you'd like to apply a custom filter to the $("#area") collection you can do so using filter. This will return a jquery object where you can get the .css("left") from.
If you'd like to save both the id's and the left property you can do so with the following code:
var objects=[];
$("#area").filter(function(){
$this=$(this);//cache the object
objects.push({id:$this.attr("id"),
left:$this.css("left")
};
});
console.log(objects);

Split function going Abnormal in JavaScript

This is the Contents of file from where i am reading...
aaa 3333,bbb 5,ccc 10
I am getting un defined for the keyvalue[2], [3], [4] and [5]. Why is it so???
I am actually first spliting based on , and then based on space.
because you split by comma first, so item is now 'PrimeSuiteId 3333'. When you split that by space you get two items only, so 3rd value (keyvalue[2]) and above is empty.
Edit: possible fix to make second part of your script work
swap
var items = contents.toString().split(',');
with
var items = contents.toString().replace(/,/,' ');
which will simply replace commas with spaces in the original string so your array of expected values matches up
Another edit: because splitting by comma or space is better (as in comments)
var contents = f.read();
Ti.API.info(contents);
var items = contents.toString(); // changed to return complete string not split
// removed for loop altogether
var keyvalue = items.split(/,|\s/); // changed to split by comma or space
var AppointmentSearchDaysAfter = keyvalue[0];
var AppointmentSearchDaysAfterValue = keyvalue[1];
var AppointmentSearchDaysBefore = keyvalue[2];
var AppointmentSearchDaysBeforeValue = keyvalue[3];
var PrimeSuiteId = keyvalue[4];
var PrimeSuiteIdValue = keyvalue[5];
From the contents in the contents file you should only be able to get values for
var AppointmentSearchDaysAfter = keyvalue[0];
var AppointmentSearchDaysAfterValue = keyvalue[1];
You only have one space for each data entry between the commas
Split function is working fine, you are expecting it to behave abnormal.
You will get only two values in array after split by space. From where will it bring 6 values!!!?
The rest values you will get in next iterations.
Instead of declaring individual variables for each item and then loading them from the contents string, you can reduce the whole thing to an object with key/value pairs:
var items = contents.split(',').reduce(function (acc, val) {
var split = val.split(' ');
return acc[split[0]] = split[1], acc;
}, {});
To test what the values are, try:
console.log(items.PrimeSuiteId); // outputs 3333
console.log(items.AppointmentSearchDaysBefore); // outputs 5
console.log(items.AppointmentSearchDaysAfter); // outputs 10

Categories

Resources