So this is the problem
Actually i have this code, to insert a value from an Select
var valorTipoMenu = document.getElementById('tipoMenu').value;
First i take the value from the select which id is 'restaurant', and later i take the value and insert on mongo collection like this,
Menu.insert({tipoMenu:valorTipoMenu});
so this work fine, but just insert 1x1 menu at time, so this is what im wanna do, i want to insert more than 2 values on valorTipoMenu variable, i think this may work like selecting one item from the select list, storing on and array variable called idk manyMenus , and later inserting that value on the mongo collection, but how can take the value from the select and storing on some array and later taking that array and inserting on mongo collection?
thanks regards
There are a number of different ways you can do this, but if you just want an array:
var valorTipoMenuArray = [];
valorTipoMenuArray.push(document.getElementById('tipoMenu').value);
valorTipoMenuArray.push(document.getElementById([NEXT ELEMENT YOU WANT TO PUT IN ARRAY]).value);
... MORE ELEMENTS ...
Menu.insert({tipoMenu: valorTipoMenuArray});
Related
trying to save the input provided from the user in an input form that is cloned, so i want it to work for all the 'save' options, keep that text in local storage, and lastly to maintain that text in the input form even when the user refreshes the page, i've tried a bunch of different ways at my wits end, ha. here is fiddle: https://jsfiddle.net/dalejohn33/24u3vxmp/13/
$save.click((f) => {
var task = $("input:selected").val();
var getTask = JSON.parse(localStorage.getItem("input"));
var setTask = JSON.stringify(localStorage.setItem("input"));
console.log("text input has been saved");
}
thanks for any help!
There are some tasks here:
Get the right input's value
var task = $(f.target).closest('.dropdown-menu').next().val();
$(f.target).closest('.dropdown-menu') gets you to the dropdown and the input is next() to it.
localStorage.setItem accepts 2 arguments: (1) The key name (2) The data (You pass only the key).
In order to set the input's value to the value stored in localStorage, you need to store each input as a different key (not just input).
You need to iterate the inputs and set its value from localStorage.
Since the value you store is string, you don't need to stringify / parse.
Another point is the inconsistency name and id. The first element (the one you clone later) has none. Also the id and name are different values because you increase it between (length++)
https://jsfiddle.net/moshfeu/kw8jfg1m/32/
I have a value that I need to compare to the values in an object. The object is like:
[{"dbid":800,"MemberID":1460,"ID":1460,"Search":"TRUE","Year_Start":"2017","Year_End":2019,"Last_Name":"XXXX","First_Name":"XXX","Middle_Initial":"X","Suffix":"","Email":"","Program_Code":"CM","Pending":"","Initials":"OS","Include":"1","Exclude":"0","Authoring_Names":""}, ... ]
and again for 100's of names.
I want to create a search box that allows the end user to compare a name to the names in the list. So I want to send the last name of the comparing value to a function that will return most of the information such as First Name, Middle Initial, Last name, Program etc. The comparing value may or may not be in the list.
I've look at
Vue JS2 find array value by id
and it's close, but I want more information than one element. Also I saw that it maybe possible to filter an object in Veux, since I store that information in there.
To find all people with a certain last name, you should use filter as it's very similar to find only it returns multiple items in an array.
const found = people.filter(({ Last_Name }) => person.Last_Name == Last_Name);
Note that to check if no people have been found you need to check if length == 0 because an empty array is still truthy.
I want to store every created field in localStorage to get the ID's for later use.
This is a cordova app.
I'm creating field with javascript. An outside DIV, and input field and a button.
This input field and button has to be stored in localStorage for later use.
I have stored the div group ID, input Field ID and button ID in localStorage. I do not know if this is the best solution, but if not, then I would appreciate some pointers.
This is how I store it:
localStorage.setItem('inputGroup', 'input_area_group_' + id + '');
localStorage.setItem('inputField', 'input_field_' + id + '')
localStorage.setItem('delButton', 'delBtn_' + id + '');
Now.
This is the first time I'm acctualy using localStorage, so I do not have that big clue about it, but at least I'm trying.
When I store these 3 values into local storage and create another field with the excact same values. Will those then overwrite the other results or will there be an array with all the results in "inputGroup" f.eks? Like if I have 40 fields, I would like to have 40 values in "inputGroup" of localstorage.
This is because when I have created these fields, I would like to delete one of them if I want to. And then I tought that I could fetch the ID in localstorage that match the ID of the button that I pressed.
Is this a viable solution?
If not, then I'm happy with some pointers to what I could use that would make this much better.
LocalStorage is a dictionary, so the first part (the key) is unique. The second part, the value is tied to the key. Changing the value portion will replace, not add a new one. You could change the key value if you wanted instead.
Or you, can store the value as a delimited string, concatenating the new string each time. (However, concatenation is generally a slow operation). In this case, you could take the concatenated string and convert it to a JavaScript array.
this is my first question on stackoverflow.
and a little bit experience on code.
so I have a localstorage like this:
myDB: "[{"key:"123","label":"abc"}]
I have a div with "abc" as value:
<div id="name">abc</div>
And many id's div clone with different value
<div id="name">abc</div>
<div id="name">cde</div>
<div id="name">efg</div>
I want to read the value of the ID "name", make a if/else like looking "abc" are in the localstorage, if yes delete it with the key. else not delete.
I have thinking of using document.getElement to get value from ID and compare it to localstorage and using if else to do that thing. But there are many clone have that event to trigger the function to delete it. So the function don't know which ID's value to be compare and delete it.
I really awkward for this newbie question. But I have to ask, many thanks first :)
*New question:
I want to delete last element of the localstorage.
Can I convert localstorage to array then using array.pop(). Then convert the changed array again to the localstorage?
First, as was mentioned by others, id must be unique. You can use any other attribute instead, for example, class:
<div class="name">abc</div>
<div class="name">def</div>
<div class="name">ghi</div>
<div class="name">jkl</div>
<div class="other">mno</div>
Then, to query these elements, you could use document.getElementsByClassName("name") which will return you an array-like object. You can convert this object to an array of values using a combination of spread syntax and map method:
let values = [...document.getElementsByClassName("name")].map(e => e.innerHTML);
To work with the local storage you can use localStorage.setItem and localStorage.getItem. As you know, the local storage stores only strings, so JSON.parse and JSON.stringify methods will be helpful too.
Here is the example of code:
localStorage.setItem("myDB", '[{"key":"123","label":"abc"}, {"key":"456","label":"mno"}]');
console.log('Local storage before: ', localStorage.getItem("myDB"));
// extracting div values to an array
let values = [...document.getElementsByClassName("name")].map(e => e.innerHTML);
// creating a js object from myDB string
let db = JSON.parse(localStorage.getItem("myDB"));
// leaving only those elements, which labels are not in the values array
localStorage.setItem("myDB", JSON.stringify(db.filter(item => !values.includes(item.label))));
console.log('Local storage after: ', localStorage.getItem("myDB"));
JSFiddle link: https://jsfiddle.net/v03wpgq1/4/
id attributes should be unique on the page, otherwise only the last one on the page has the ability to be referenced easily (or at least, properly).
There should be a section that contains these which makes them easily queryable. Perhaps inside of an element with an id guaranteed to uniquely hold a set of name value pairs.
Using document.querySelectorAll is your best bet for finding these elements, and will be made easier by creating a structure that can be queried.
With a set of items to look for, it should be easy to iterate and test for values and keys inside of localStorage.
This is javascript array
var data =['athar','naveed','123','abx'];
Now I want to access this array in code behind array or list variable. Don't want to use Hidden field.
If you want to use in anyother javascript function,you can simply use data[0] to access first element i.e.,athar.
data.length will give you the count of values present in the array.