/    Sign up×
Community /Pin to ProfileBookmark

How to convert this jQuery snippet to vanilla JavaScript

Hi,

While I’m learning JavaScript, I started to work on the following example

[URL=”https://codepen.io/itsthomas/pen/wxwRYx?editors=1010″]https://codepen.io/itsthomas/pen/wxwRYx?editors=1010[/URL]

The script sends an Ajax call and gets back a simple JSON file.
I fetch the response and build a simple unordered list using Handlebars.js.
So far so good.

Now I wanted that if I click on the button in front of each character, a hidden div becomes visible, which contains some details about the character.
I achieved this easily using jQuery.
However, I spent a whole day to convert that small jQuery snippet to vanilla JS and failed badly. πŸ™

How can I change the jQuery snippet, which shows and hides the div to vanilla JS?
It’s not important if it slides or not. It would be enough if I could just show and hide it by adding the class [B]visible[/B] to it.

This is the jQuery code, I want to convert to vanilla JS:

[CODE]
// jQuery solution using event delegation for slideToggle
$(‘#container’).on(‘click’, ‘button.btn’, function() {
console.log($(this)); // this is the button with the class btn

// If the button is clicked, add a class to .details
$(this).parent().parent().next($(‘.details’)).slideToggle();
})
[/CODE]

to post a comment
JavaScript

8 Comments(s) ↴

Copy linkTweet thisAlerts:
@rootJul 09.2018 β€”Β I guess you can work it out from here...

https://www.w3schools.com/jsref/met_document_addeventlistener.asp

then a basic function reference by name that you put in to place the code you want run.
Copy linkTweet thisAlerts:
@Frank62Jul 20.2018 β€”Β I've been writing vanilla JS all my life, so I should be able to help you. But your post doesn't give me enough info. Please make minimal but functional code example, from <doctype html> to </html> and post it here, plus a JSFiddle (https://jsfiddle.net/).
Copy linkTweet thisAlerts:
@rootJul 20.2018 β€”Β @Frank62#1594088 first off its <DOCTYPE not <doctype people using <doctype are leaving their pages open to being run in quirks mode with unexpected results All browsers understand <DOCTYPE but very few understand <doctype

second, for what the OP wants requires no examples other than what they have provided already.
Copy linkTweet thisAlerts:
@Frank62Jul 20.2018 β€”Β > @root#1594103 All browsers understand <DOCTYPE but very few understand <doctype

Not true. I did make a mistake by forgetting the exclamation mark (it should be <!doctype html>), but upper or lower cast doesn't make a difference. That was a long time ago.

>@root#1594103 second, for what the OP wants requires no examples other than what they have provided already.

I'm looking forward to your answer then.
Copy linkTweet thisAlerts:
@rootJul 21.2018 β€”Β All non HTML5 browses dDO NOT understand <!doctype.

ALL BROWSERS understand <!DOCTYPE The XML serialization of HTML5, aka 'XHTML5': Note that if you don't uppercase DOCTYPE in an XHTML document, the XML parser will return a syntax error[/quote] and I do know that the W3C had a page stating that <!doctype should not be used, its <!DOCTYPE

As for you other comment, what answer an I giving? I stated that they provided code that they want as regular JavaScript
Copy linkTweet thisAlerts:
@Frank62Jul 21.2018 β€”Β > @root#1594112 All non HTML5 browses dDO NOT understand <!doctype.

Such as IE5?

>root#1594112 As for you other comment, what answer an I giving?

None, and that's the problem. I'm asking for more info, you're saying that's not necessary, that it can easily be done with the given code, but you're not living up to it.
Copy linkTweet thisAlerts:
@rootJul 21.2018 β€”Β Excuse me? Not living up to what?

I have pointed the original poster of the resource, if you wish to add a response that helps the original poster, then carry on.


Copy linkTweet thisAlerts:
@Jeff_MottJul 22.2018 β€”Β The OP asked this same question on another board, and I'd like to share the answer from there to give the folks here an example of what a good and useful reply looks like.

https://www.sitepoint.com/community/t/how-to-convert-this-jquery-snippet-to-vanilla-javascript/301396
Γ—

Success!

Help @kayut spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 β€” BETA 5.18,
whats_new: community page,
up_next: more Davinciβ€’003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinciβ€’003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...