Count element selected in nested json [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have to count length of a jsonArray but i'm able to do it. This is the example from start:
https://jsfiddle.net/vuqcopm7/13/
Summary, if you click over an item of the list, for example "asd1" it will create an input text for each time you click that item or the others. What i need is count how many times that item is clicked, or better, how many input that item creates. Is it possible? For example instead:
asd1 (numberForEachItemSelected)
if i create 2 input, because i tap over it 2 times, it will be:
asd1 (2)
everything in angularjs

Updated Fiddle
I changed the push pushItems function to increment a 'clicks' key I added to 'attributes', then I bound that to the view:
$scope.pushItems = function pushItems(attribute, items) {
items.clicks++
//do stuff..
}
If you can't modify your model to add the 'clicks' key server side, adding this if statement will either add to the clicks key, or set it if it doesn't exist:
$scope.pushItems = function pushItems(attribute, items) {
if (items.clicks) {
items.clicks++
} else {
items.clicks = 1
}
// do stuff
}

Related

Html5 Form reset button remembers what each form field was loaded with, is that info availble [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Can I access these values to implement my own custom reset function since I cannot use the standard reset functionality, or do I have to create hidden field to stores these values myself, or can I store the initial data in the client memory somehow as the page is loaded that can then be used by custom reset function.
Update
I have it working for text fields but it doesnt worked for checked values, what am i doing wrong
function resetOptions(divName) {
var inputs = document.getElementById(divName).getElementsByTagName('input');
for (i = 0; i < inputs.length; i++) {
if(inputs[i].type=='checkbox')
{
if(inputs[i].defaultChecked==true)
{
inputs[i].setAttribute('checked', 'checked');
}
else
{
inputs[i].removeAttribute('checked');
}
}
else
{
inputs[i].value=inputs[i].defaultValue;
}
}
}
You are looking for the properties defaultValue and defaultChecked.
https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement

I need to programatically create several variables in javascript? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I asked this question but it was marked as duplicate - it's not.
I need to programmatically create X number of variables (the number will be the value of someArray.length - which is also something created programmatically and thus unknown to me).
Is there any way to say "there's 8 items in this set, so I need 8 variables, and I need to be able to call on them/manipulate them later?" Hopefully I'm making sense.
My core problem: I programmatically generate a list of links. Can be any number of links. I need to be able to validate that the user clicks each of these links before advancing.
I programmatically generate a list of links. Can be any number of links. I need to be able to validate that the user clicks each of these links before advancing.
You do not need to create an unknown number of variables to solve your problem (how would you name them?). As stated by other commenters, you have a list of links, and you need to keep track of which ones have been clicked.
There are numerous ways to solve this problem in JavaScript. Below is an example of a straightforward approach which should be easy to follow. The approach is simply to use another array, linksClicked, to keep track of which links have been clicked. To validate, count the number of links that have been clicked and compare to the total number of links.
var arrayOfLinks = [
'http://www.stackoverflow.com',
'http://www.arstechnica.com'
];
var linksClicked = [];
function clickLink(url){
//check if link is in arrayOfLinks
for(var i = 0; i < arrayOfLinks.length; i++){
//if link is in arrayOfLinks, mark it as clicked
if(arrayOfLinks[i] === url){
linksClicked[i] = true;
}
}
}
function checkLinksClicked(){
//count number of links that have been clicked
var linkSum = 0;
for(var i = 0; i < linksClicked.length; i++){
if(linksClicked[i]){
linkSum++;
}
}
return linkSum;
}
console.log(checkLinksClicked());
clickLink('http://www.stackoverflow.com');
console.log(checkLinksClicked());
clickLink('http://www.stackoverflow.com');
console.log(checkLinksClicked());
clickLink('http://www.arstechnica.com');
console.log(checkLinksClicked());

Push values into an array and refresh that array when another selection is made [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have three select lists that are filled with table names, column names and attribute names. I push them to a variable like this:
data.forEach(function(field){
fieldList.push(field);
But if I change a select list it adds all the column names of the second table to the array like this:
Here u you can find an exmample of what I want.
If you simply want data of the last selected table, clear array on your event.
fieldList = [];
data.forEach(function(field){
fieldList.push(field);
}
I fixed the problem. Just needed to add this line infront of the code:
fieldList.length = 0; . This line empty's the array before it gets filled again.

Can someone explain to me why arguments within functions immediately get assigned the value of the object at hand? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Consider the code below:
// Get Index of object based on id value
var arrayPosition = userListData.map(function (arrayItem) {
return arrayItem.username;
}).indexOf(thisUserName);
Why is it that arrayItem = userListData[0], userListData[1], userListData[2]... ?
In general, they do not.
That is just what the map function is designed to do.
callback —
Function that produces an element of the new Array, taking three arguments:
currentValue — The current element being processed in the array.
etc

Apply focus to one object, after 10 seconds apply focus to another object, is that possible? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I want to perform a set timeout function with this behavior:
Apply focus to one object, after 10 seconds apply focus to another object, is that possible?
So I want to called it like
focustimeout([objectsarray])
where objectsarray is an array with all the id names for the N fields that I want to have a 10 second focus before each other.
I'm sorry for the question but in really new on javascript.
What can I add to this in order to get that solution
var temp = "id1";
var temp1 = "id2";
setTimeout(function(){
$("#"+temp).focus()
setTimeout($("#"+temp1).focus(), 10);
}, 10);
It's possible. Here is an idea how you can do it. Create function with 2 parameters, number of focused element and array of IDs which you want to focus.
function focus(i,array){
$("#"+array[i]).focus();
if(i<array.length){
setInterval(function(){focus(i++,array),yourTime);
}
Then call it from beginning focus(0,array)

Categories