How to make HTMLCollection not live? [duplicate] - javascript

This question already has answers here:
Fastest way to convert JavaScript NodeList to Array?
(15 answers)
Closed 5 years ago.
How to make HTMLCollection not live?
var cells = someTable.getElementsByTagName('td');
Is live collection, meaning that when I add new td to table, cells length will increase by 1. How can I make it not live?

You have a couple of options.
One is to use querySelectorAll instead; it returns a "snapshot" NodeList:
var cells = someTable.querySelectorAll("td");
Another option is to convert it into an array:
var cells = Array.from(someTable.getElementsByTagName("td"));
Array.from is relatively new, but can be polyfilled for older browsers. Or if you want to support old browsers without polyfilling, use slice:
var cells = Array.prototype.slice.call(someTable.getElementsByTagName("td"));

Related

Javascript Exploring an Element's Children [duplicate]

This question already has an answer here:
How can I iterate through an Elements child nodes using Javascript?
(1 answer)
Closed 4 years ago.
So lets say I have an element that contains this:
How would I be able to run through all these children in javascript?
Use querySelectorAll (which can be directly iterated over, unlike the HTMLCollection methods):
document.querySelectorAll('table.table').forEach(table => {
// do something with each table
});
To iterate over tables seems not useful but here it is. Note I am using the class name for your tables above to find each one. If your example had different classes for each table this would not work.
var tables = document.getElementsByClassName("table");
var arr = new Array();
for (i = 0; i < tables .length; i++) {
//do what you want here
}
If you want to work with the rows and cells within a table you might want to refer to this solution.

DOM equivalent of jQuery .clone() function [duplicate]

This question already has an answer here:
How to duplicate a div in JavaScript
(1 answer)
Closed 5 years ago.
What is a good equivalent for the jQuery .clone() function in regular DOM JavaScript? I performed multiple searches (on both SO and Bing) and didn't find a specific answer. I need to produce a copy of an element and all of its internal elements. The clone must have all of the elements and content of the source elements. If possible, make the solution as compact or efficient as possible.
try this
var clonedElement = document.getElementById('id').cloneNode(true)
var element= document.getElementById("myid");
var clone= element.cloneNode(true);

adding jquery selector objects into a single jquery object [duplicate]

This question already has answers here:
Combining selectors?
(3 answers)
Closed 6 years ago.
I need to combine two jquery selectors into a single Jquery object.
I tried $("#selector","#selector"), but its not working and returning blank, do we have any predefined methods to achieve this.
Something like
var combined_Jquery_obj =$("#selector") +$("#selector")
Use add() method when you want to add elements to an existing jQuery object.
var combined_Jquery_obj = $("#selector1").add("#selector2")
Or use comma separated multiple selectors when you want to select multiple selectors.
var combined_Jquery_obj = $("#selector1,#selector2")
Use Multiple Selector
var combined_Jquery_obj =$("#selector,#selector")

Delete random objects from array [duplicate]

This question already has answers here:
Picking 2 random elements from array
(10 answers)
Closed 7 years ago.
I have an array such as:
array=['a','b','c','d','e','f'];
I want to delete a random 2 elements. How can I do this?
To get two unique items from the array, and if you don't mind mutating the original array, you can use splice() to remove the selected item from the array so it won't be picked when you run it a second time:
var firstRandomChoice = array.splice(Math.floor(Math.random()*array.length), 1);
var secondRandomChoice = array.splice(Math.floor(Math.random()*array.length), 1);
If you use a utility library such as lodash, you may already have a function available to do this for you. For example, lodash provides sample(). So if you were using lodash, you could just do something like this to get an array of two random items:
var results = _.sample(array, 2);

jQuery (or JS) How to make a Randomized New Array From an existing one [duplicate]

This question already has answers here:
Sampling a random subset from an array
(15 answers)
Closed 8 years ago.
Say I had
var imgs = ["pItem1","pItem2","pItem3","pItem4","pItem5"]
How can get a new array from the current one, that randomly picks lets say 3 items from the old array and puts it in a new array.
var newArray =["pItem1," "pItem4," "pItem2"];
You're basically looking to take a randomly sampled subset of an array. One approach is to randomly shuffle the array, then take a slice from the beginning of the array. See this implementation of getRandomSubarray() in another answer: https://stackoverflow.com/a/11935263/2943575

Categories

Resources