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 8 years ago.
Improve this question
How can I go through a certain ul-Element and search every li-Element in it? I've only found jquery solutions, but don't want to use it here.
Looping through a selection without some external library such as jQuery or D3 is kind of annoying because the selection will not return an Array but instead an HTMLCollection or NodeList (depending on your browser). I recommend using some third party library if you are doing this sort of thing often. Anyway here's how to do it in pure JavaScript.
// This is not an Array but either an HTMLCollection or a NodeList
var selection = document.getElementById(<id>).getElementsByTagName("li");
for(var i = 0; i < selection.length; i++)
{
// do something with selection[i]
}
It is important to note that because your selection is not an Array you cannot use for-each loops (the intended way at least) or any higher level Array functions as described here.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed last month.
Improve this question
Im working with javascript but maybe there is also an general answer.
Assuming we have 10 objects with ids.
Would it be faster to search for an object with an specific id by using the array find() methode:
const found = entries.find(entries => entries.id === myId)
or initial collect the data in a Map and use the get() methode?
Thanks to the comments I know now:
Array.find() is similar iterating/looping through an array and that means, it is slower then using get(), which is not a loop.
So if I want to search only one time in this array, find() should be enough. If I want to find multiple elements in that array. It makes sense to generate a Map from that array.
PS: The amount of 10 was an example. I just wanted to know what generally is faster.
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 1 year ago.
Improve this question
I have an array of objects in Javascript (I use Vue.js2 in my project) and with .splice I can easily delete all the elements inside of it. The problem is, when only one element remains, I can't delete it because it stays there no matter what.
The easiest way to clean up an array is to make it [array].length = 0
You can use .length, .filter, .pop and im pretty sure you can .splice the last element, maybe there is something else u havent noticed yet.
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 7 years ago.
Improve this question
I just found This and it has some comparation with JQuery and others...
Its really faster than the others.
What you guys think about this. Is it ok to use only this? Is there anything that you will HAVE/NEED to use JQuery ?
Also, when it comes to performance. Is there a big difference between:
var test = document.getElementById('test-table');
test.attr('id','123');
var test = document.getElementById('test-table');
test.dataset.id = '123';
It doesn't matter since you can always wrap any DOM element around a jQuery object if you must.
var test = document.getElementById('test-table');
// Do some vanilla stuff
var jTest = $(test);
// Do some jQuery
The jQuery library builds upon the DOM API that is available to JavaScript. The only reason you'd need jQuery is to do a complex task that requires more effort in vanilla. In terms of performance, the difference is negligible. jQuery adds checks to be cross-browser compatible. If you code to modern standards, these checks are not necessary.
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 9 years ago.
Improve this question
i mean i wanna iterate manually using a for-loop or something. but this piece of code i came up with seems to be not working. i like combining javascript with jquery since jquery is not my cup of tea for major projects. i don't know much jquery either, i would say I'm beginning to learn, though. how do you iterate over a nodelist in jquery is the question i have for all those jquery fans this time. is it similar to the javascript way? anyway this is what i have come up with (the code of a beginner).
$("sn"[i]).fadeIn();
$("sn"[i]) the part which failed, according to google chrome.
try this:
$("sn[" + i + "]").fadeIn();
I think you mean that "sn" is the selector for the nodes, in that case:
$("sn").fadeIn();
This works on all the elements that match the selector, jQuery will do the iteration. However if you want to select all elements that have the 'sn' class you should prefix the selector with a . like so: ".sn"
if you want to loop manually try:
$(".sn").each(function(i) {
$(this) // do some magic with the individual element here
});
See more on iterating with each here:
https://api.jquery.com/each/
Assuming sn is a variable containing the node list, you are probably looking for
$(sn[i])
or
sn.eq(i)
if sn is already a jQuery object.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I'm sure there are many questions and articles on the subject, I just cant seem to find exactly what I'm looking for...
I have a form where one of the fields excepts a number (1 to 30), I the want to create x elements, where x is the number selected, all elements should have about the same HTML structure, differing only in their id's. the elements are complex, and I think I should format them using HTML/CSS, as I feel that js should only hold the business logic, sort of MVC style.
I suppose my question is two fold:
Is a "philosophical" question: Is it a good idea to try and keep the "view" (format/layout/design) in the HTML/CSS and the "control" (logic) in js? I'm new to web dev, and I'm not sure of web dev architecture philosophy...
Considering I want to keep formatting out of my js, how do I go about dynamically adding/duplicating HTML elements, whilst keeping the HTML in the same file where it belongs. My first thought was to create as many elements as I might need, hidden, and and show the right number of them when needed, but this can't be the best way to do this... My second thought was to use the jquery.clone() function, but this would create duplicate id's.
thanks.
I think you would really benefit from using a client side compositing framework (like KnockoutJs, but there are loads of others like Ember, Angular).
That way you can keep your model (viewmodel in the case of Knockout) in JavaScript and only worry about the implentation of the view in your templated HTML.
Check out the cool knockout tutorials, maybe its what you're looking for
http://learn.knockoutjs.com/
var inputCount = paraeInt$('input').val()),
elementMarkup = $('<div class="dynamic- div"/>');
for(var i; i < inputCount; i++){
$('body').append(elementMarkup);
}