-3
I am starting my studies with Typescript and I am trying (and need) to modify a code that I found ready, which is in Jquery. But I need it all in Typescript, but I’m not able to make it work yet. Someone there can give me a help ?
The link from where I found the code is that.
There is code in Jquery working and html and css to test.
For ease, here is the code in Jquery:
$('.dropdown-menu a.dropdown-toggle').on('click', function(e) {
if (!$(this).next().hasClass('show')) {
$(this).parents('.dropdown-menu').first().find('.show').removeClass("show");
}
var $subMenu = $(this).next(".dropdown-menu");
$subMenu.toggleClass('show');
$(this).parents('li.nav-item.dropdown.show').on('hidden.bs.dropdown', function(e) {
$('.dropdown-submenu .show').removeClass("show");
});
return false;
});
Code I’ve tried to make: (getParents function I took from that question.)
function getParents(parentSelector) {
// If no parentSelector defined will bubble up all the way to *document*
if (parentSelector === undefined) {
parentSelector = document;
}
var parents = [];
parents.push(parentSelector); // Push that parentSelector you wanted to stop at
return parents;
}
var addDropDown = document.querySelectorAll('.dropdown-menu a.dropdown-toggle');
for (var i = 0; i < addDropDown.length; i++) {
addDropDown[i].addEventListener('click', function (e) {
if (!this.nextElementSibling.classList.contains('show')) {
var elemento = getParents(this)[0].querySelectorAll('.show');
for (var z = 0; z < elemento.length; z++) {
elemento[i].addEventListener('click', function (e) {
elemento[z].classList.remove("show");
});
}
}
var $subMenu = this.nextElementSibling(".dropdown-menu");
$subMenu.classList.toggle('show');
this.parentNode('li.nav-item.dropdown.show').addEventListener('hidden.bs.dropdown', function (e) {
var itensLista = document.querySelectorAll('.dropdown-submenu .show');
for (var x = 0; x < itensLista.length; x++) {
itensLista[i].classList.remove("show");
}
});
return false;
});
}
Console error:
Uncaught Typeerror: this.nextElementSibling is not a Function At Htmlanchorelement.
The line indicated by the error is this:
var $subMenu = this.nextElementSibling(".dropdown-menu");
NOTE: I AM NOT USING JQUERY
Why can’t you make it work? Compiler shows some error?
– Andre
@user140828, I still don’t quite understand Typescript, that’s why kk. I’m not now with the code I was trying, but then I leave on the question... but I didn’t go too far no. Better start from scratch.
– user153127
@user140828, I edited the question by placing the code I have already made, and I will leave what error is being presented in the compiler
– user153127
The last fragment does not help at all because in Typescript depending on where the keyword is used
this
it takes different values. In a constructor, member function, or propertythis
points to the current instance of the class in which it is used. In a static method or static propertythis
points to the constructor of the class in which it is used. In a function declarationthis
points to the scope of the function itself in which it is used. This exists as a parameter possessing its own logic. Edit the question put the full declaration of the element where the problematic 'this' occurs.– Augusto Vasques
I appreciate the comment, but could I leave an answer as to what the code would look like with all this in mind? @Augustovasques
– user153127
As I explained without knowing what the context is
this
and without knowing which object it references is impossible to answer the question.– Augusto Vasques