How to get element/component name using JavaScript? - javascript

I have a webpage that contains ASP.NET panels and JavaScript that gets all of the components in the page:
var items = Sys.Application.getComponents();
I need to get the name/ID of each of the elements in the items variable. I have tried the following code: (but it doesn't work)
for (var item in items)
{
alert(item.name);
}
What attribute of 'item' contains the name/ID? How can I modify the above code to do this?

Use the get_name() and get_id() functions instead.
var items = Sys.Application.getComponents() ;
for( var i = 0 ; i < items.length; i++ ) {
var item = items[i];
var id = item.get_id();
var name = item.get_name();
}
http://ajax.asp.net/docs/ClientReference/Sys/ApplicationClass/SysApplicationGetComponentsMethod.aspx

None. item is the key/property so you want items[item].name.
However, if items is an array, using for..in to iterate over it is completely inappropriate. In this case use for(var i = 0; i < items.length; i++) and var item = items[i] inside the loop body.

Related

How can I delete array elements just once to add new ones with a for loop

What I am trying to do is:
set an array value (list) to another array (options).
If the user's input (searchVal) matches with a list value it will delete options, push this match, and then will keep pushing the next matches without deleting options again.
So according to the code below, if searchVal was "whatever", options should return: ["whatever", "whatevEver1"] but, instead, it returns: ["whatever", "WhatEver1", "whatttever", "whatever", "whatevEver1"]
Relevant code:
var list = ["whatever", "WhatEver1", "whatttever"];
var clear = 0;
var options = [];
for (var i=0 ; i < list.length ; i++)
{
options.push([list[i]]);
}
var searchVal = window.prompt(" ");
for (var i=0 ; i < list.length ; i++)
{
if (list[i].toLowerCase().includes(searchVal.toLowerCase())) {
if (clear == 0) {
options.length = 0;
}
options.push([list[i]]);
}
clear++;
}
return options;
Js arrays are pass-by-reference. In order to make independent copy of array you need to use:
let options = JSON.parse(JSON.stringify(list));
I didnt try to implement this to your problem cause im too lazy but i think it might work.

Append param on loop javascript

I have a function that has a menu param
removeChildCheck:function(menu){
let removeArrayValues = [];
for(var i=0; i < this.checkbox.menu.length; i++){
removeArrayValues.push(this.checkbox.menu[i].value);
}
this.user.permissions = this.user.permissions.filter((i) => !removeArrayValues.includes(i));
},
when I used it like this removeChildCheck('dashboard') im getting length of undefined.
How can I append the param and loop on it? TIA
Do you mean to do:
this.checkbox[menu]
That is, access the property of checkbox that has the name stored in the variable menu? Keep in mind that:
this.checkbox.dashboard
is equivalent to
this.checkbox['dashboard']
and
menu = 'dashboard'
this.checkbox[menu]
As audiodude says, use array syntax: this.checkbox[menu].length
removeChildCheck:function(menu){
let removeArrayValues = [];
for(var i=0; i < this.checkbox[menu].length; i++){
removeArrayValues.push(this.checkbox[menu][i].value);
}
this.user.permissions = this.user.permissions.filter((i) => !removeArrayValues.includes(i));
},

multiple document.getElementById for var

Hello i want to use "var" on multiple Id´s.
// Get modal element
var modal = document.getElementById('modal1');
// Get open modal button
var modalBtn = document.getElementById('Info1');
// Get close button
var closeBtn = document.getElementById('closeBtn1');
This is the code that works, but i want to do is that it is "modal1, modal2 ... and Info1, Info2...
Can you help me with that?
Hey Celtor you can find basic knowledge about JavaScript Variables
in here.
then to do so you can use for loop with array as so:
// Create list of modals with 15 undefined elements
var modals = Array(15);
for (i = 0; i < x.length; i++) {
// Assign the elemets to the list in their index
modals[i] = document.getElementById('modal'+i);
}
Hope this helps!
You will have to use a function like this:
var getElementsByIds = function(ids){
var elements = [];
for(var i = 0; i < ids.length; i++){
elements[ids[i]] = document.getElementById(ids[i]);
}
return elements;
}
This will return an array in which each key is an element you specified, and the key is the element's ID.

Is there a way to shorten a long array name without assigning it to a new array?

A json response has an array of objects that I want to iterate without having to use the full (I don't know what you would call "results.data.messages.items") long name:
for (var i = 0; i < results.data.messages.items.length; i++) {
console.log( results.data.messages.items[i].id );
}
I know how to iterate over items, but I can't find any way to shorten that down so I don't have to type results.data.messages.items[i].property_name each time and instead could just do item[i].property_name.
I don't want to duplicate the array just for the purpose of having a shorter name.
Assigning the array to a new variable does not duplicate the array—it still refers to the same object. The new variable works sort of like an "alias". Therefore, this is absolutely fine:
var element = results.data.messages.items[i];
Alternatively, you could declare a new variable outside the loop:
var items = results.data.messages.items;
Why don't you shorten it to the element at the specific index, instead of the array:
for (var i = 0; i < results.data.messages.items.length; i++) {
var item = results.data.messages.items[i];
console.log(item.id);
}
EDIT
To keep it simpler, you could iterate through it using the array protoype method forEach, that already assigns each element with any name as you wish:
results.data.messages.items.forEach(function(item) {
console.log(item.id);
});
You could always define these in the for loop's definition
for (var i = 0, items = results.data.messages.items; i < items.length; i++) {
console.log( items[i].id );
}
There is also the usually avoided with
with( results.data.messages ){
for (var i = 0; i < items.length; i++){
console.log( items[i].id );
}
}
Assign it to a separate variable before your loop:
var items = results.data.messages.items;
for (var i = 0; i < items.length; i++) {
console.log( items[i].id );
}
This does not create a new array. Just a new variable that references the same array.
Another option would be to use array's builtin methods to reduce duplication:
results.data.messages.items.forEach(function (item) {
console.log(item.id);
});

obtaining all items of listView in winjs

I have a listview div on screen and I have added itemDataSource to it successfully
var lettersList = new WinJS.Binding.List(jsonArrayForClearance);
var list_ = document.getElementById("prodListView").winControl;
list_.itemDataSource = lettersList.dataSource;
list_.itemTemplate = document.getElementById("tileTemplate");
list_.forceLayout();
Now in each item I have added a custom input type for user to specify(using template). I want to iterate through all the items of list and obtain the value of that input type in an array.
how can I do it?
EDIT: My question is to access custom input type declared in list items. As such we use following code to access any input type named "inpT"
document.getElementById('inpT');
but how to access the same from list item? can I use Following code(as suggested by user2608614)
var listView = document.getElementById("prodListView").winControl;
var list = listView.itemDataSource.list;
for (var i = 0; i < list.length; i++) {
var item = list.getAt(i);
item.getElementById('inpT');
}
You can iterate through the list elements like this:
var listView = document.getElementById("prodListView").winControl;
listView.itemDataSource.getCount()
.done(function(count) {
for (var i = 0; i < count ; i++) {
listView.itemDataSource.itemFromIndex(i)
.done(function (item) {
//***item will contain your property
});
}
});
Is not the best solution as it make it difficult to chain the promises, I'm also looking for a better one. But it works.
Since you're using a Binding.List you can just iterate through that much like an array.
var listView = document.getElementById("prodListView").winControl;
var list = listView.itemDataSource.list;
for (var i = 0; i < list.length; i++) {
var item = list.getAt(i);
// do something with this item
}
The only thing to remember is that it doesn't support [] and instead you have to use .getAt() and .setAt().

Categories