Please convert following code into javascript. How?
$(".plus").click(function(){ $(this).toggleClass("minus").siblings("ul").toggle(); });
Please try following link may this will help you !
Convert jQuery to JavaScript
jQuery already is JavaScript. Do you mean Vanilla JavaScript? If so, here you go:
siblings() function, so we have to implement ittoggle() function, so we have to implement itsiblings() - which would reduce the Vanilla JS code by 50% LoCelement.classList.toggle("show")const getSiblings = function(element, selector) {
// setup siblings array and get the first sibling
const siblings = [];
let sibling = element.parentNode.firstChild;
// loop through each sibling and push to the array
while (sibling) {
if (sibling.nodeType === 1 && sibling !== element && sibling.matches(selector)) {
siblings.push(sibling);
}
sibling = sibling.nextSibling
}
return siblings;
};
// unfortunately, I don't know the element type, which would have been great
// replaces $(".plus")
const plusElement = document.querySelector(".plus");
const toggleDisplayStyle = function(elements) {
// loop over all elements
for (const element of elements) {
// and toggle the display style property
element.style.display = element.style.display === "none" ? "block" : "none";
}
};
// Add an event handler to the element
// replaces .click()
plusElement.addEventListener("click", event => {
// replaces $(this).toggleClass("minus")
plusElement.classList.toggle("minus");
// replaces .siblings("ul").toggle()
toggleDisplayStyle(getSiblings(plusElement, "ul"));
});
Michael P
Good Code
Got a free tool to convert JQuery to Javascript I made for a client upgrading Ruby. He allowed me an exception on the work to hire agreement so that I could publish the tool for all you to use. Give it a try.