Home › Forums › JavaScript › Define variable inside loop and use it outside loop
- This topic is empty.
-
AuthorPosts
-
August 16, 2014 at 8:15 pm #179309
David Hosanna
ParticipantHi guys, i new to javascript. I wanted to make my own javascript library using
document.querySelectorAll
, querySelectorAll is NodeList. I want the javascript work like this:class = document.querySelectorAll(className); for(var i = 0; i < className.length; i++) { class = class[i] } // So i can use modified class variable like $example.prototype = { textNode: function(text) { class.textContent = text; } }
Unfortunately, not working. Help me to fix that, or other alternative i can use.
August 16, 2014 at 11:39 pm #179314__
ParticipantThink about what you are doing:
- you’re assigning a node list to
class
. - you’re looping over
class
, and assigningclass
to one of its indexes. This is the first problem: it will only work the first time, since by the time the second loop executes,class
is no longer a list (it’s now a dom element) and doesn’t have any indexes. - If you straighten this out, then
class
will be available outside the loop — but it will be whatever final value it was assigned in the loop. The other values will be gone.
As a side note, why are you using the length of
className
to determine how many times you loop overclass
?It’s probably a bad idea for a method of your $example prototype to rely on a global variable (i.e., something which may or may not exist). I don’t understand exactly what you’re trying to do, but it seems like something you should pass to the function as an argument.
→ Perhaps you could describe what you’re actually trying to do in more detail. What do you need to do to each item of
class
in the loop? What do you need to do withclass
afterwards?August 17, 2014 at 3:23 am #179327David Hosanna
ParticipantMy target is like jQuery selector,
$(‘foo, bar’).doSomething();
But using
document.querySelector('foo, bar')
is not working. I want to select all elements inside.August 17, 2014 at 11:40 am #179373__
ParticipantWell, if you want to have jQuery-like methods, you might look at how jQuery does it (or similar libraries, like the one Atelierbram suggested).
The main thing to realize is that
$
[orjQuery
] is a function, just like any other. When you call$
, it returns an instance of a jQuery object that has all of the DOM elements that matched the selector you used. When you call a jQuery method (or plugin) on the object, they iterate over this list of elements and do {whatever} to each one in turn.August 18, 2014 at 5:42 am #179441David Hosanna
ParticipantThanks, like i said i new to javascript. I don’t know what to do next. can you help me fix these code so it should work like jQuery selector.
function $(e) { if(e) { if(window === this) { return new $(e); } a = document.querySelector(e); return this; } } $.prototype = { // EventListener engine on: function(d, e) { if(a.addEventListener) { a.addEventListener(d, e, false); } else if(a.attachEvent) { a.attachEvent(d, e); }, textNode: function(d) { a.textContent = d; return this; } }
Thanks again _
August 18, 2014 at 10:58 am #179506__
ParticipantThanks, like i said i new to javascript. I don’t know what to do next.
Perhaps you should focus on learning javascript more thoroughly before tackling a project like this.
- you’re assigning a node list to
-
AuthorPosts
- The forum ‘JavaScript’ is closed to new topics and replies.