Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMarcelo Retana
2,534 PointsHow advanced you think this JavaScript code is?
I mean, is this for you a very advanced JS code?
This is an accordion Plug In made by me and my teacher.
var opts = {divId:'divMiAccordion',
displayMode: 'single',
titleEvent: 'mouseover'
};
var acc1 = new Accordion(opts);
var acc2 = new Accordion({divId:'divMiAccordion2',
displayMode: 'multiple',
titleEvent: 'click'});
function Accordion(opciones){
var self = this;
this.init = function(){
this.container = document.getElementById(opciones.divId);
this.contenedores = this.container.querySelectorAll('div.panel-body');
this.titulos = this.container.querySelectorAll('h3');
this.options = opciones;
for(var i=0; i<this.contenedores.length; i++){
this.contenedores[i].classList.add('hidden');
}
for (var i = 0; i < this.titulos.length; i++) {
if(this.options.titleEvent === 'click'){
this.titulos[i].onclick = tituloEvent;
}
else{
this.titulos[i].onmouseover = tituloEvent;
}
};
} //fin init
this.getId = function(){
return self.container.id;
}
function tituloEvent(){
var tempNode = null;
if(self.options.displayMode === 'single'){
for (var i = 0; i < self.titulos.length; i++) {
tempNode = self.titulos[i].parentNode.nextElementSibling;
tempNode.classList.add('hidden');
};
}
tempNode = this.parentNode.nextElementSibling;
if(tempNode.classList.contains('shown')){
tempNode.classList.add('hidden');
tempNode.classList.remove('shown');
}
else{
tempNode.classList.add('shown');
tempNode.classList.remove('hidden');
}
}
this.init();
} //fin accordion
4 Answers
Jimmy Hsu
6,511 PointsIt's not what I'd consider very advanced JavaScript code. Seems closer to intermediate, as you haven't even entered the territory of prototypes or even callbacks.
Don't get me wrong through, it's good code from what I could skim and very clean.
James Barnett
39,199 PointsBeginner, it's basic functional JavaScript.
You are using functions, conditionals, for loops and the this
keyword.
Gregg Mojica
11,506 PointsBeginner - Intermediate Javascript code. It's not super advanced.
Marcelo Retana
2,534 PointsThank you for all your answers.
I've been struggling with JS for the last 5 months, and now, I feel as if I am dominating the Objects.
What I don't know yet, is how to use Objects with forms and stuff like GETing and POSTing things to the server. I need a course on that, that I think is not on Treehouse yet.
Gregg Mojica
11,506 PointsUsually, that's done with a server side language like PHP