{"id":16419601,"url":"https://github.com/gowthamand/javascript-without-jquery","last_synced_at":"2025-07-29T22:34:21.760Z","repository":{"id":110397800,"uuid":"219748085","full_name":"gowthamand/javascript-without-jquery","owner":"gowthamand","description":"javascript-without-jquery","archived":false,"fork":false,"pushed_at":"2019-11-05T13:11:54.000Z","size":3,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-24T13:48:03.873Z","etag":null,"topics":["dom-manipulation","event-listeners","hiding-elements","jquery","without-jquery"],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gowthamand.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-11-05T13:10:56.000Z","updated_at":"2021-02-25T09:24:24.000Z","dependencies_parsed_at":null,"dependency_job_id":"2f089515-d706-405f-9b5c-6262e9ec6dce","html_url":"https://github.com/gowthamand/javascript-without-jquery","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/gowthamand/javascript-without-jquery","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gowthamand%2Fjavascript-without-jquery","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gowthamand%2Fjavascript-without-jquery/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gowthamand%2Fjavascript-without-jquery/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gowthamand%2Fjavascript-without-jquery/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gowthamand","download_url":"https://codeload.github.com/gowthamand/javascript-without-jquery/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gowthamand%2Fjavascript-without-jquery/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267772327,"owners_count":24142087,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-29T02:00:12.549Z","response_time":2574,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dom-manipulation","event-listeners","hiding-elements","jquery","without-jquery"],"created_at":"2024-10-11T07:25:17.332Z","updated_at":"2025-07-29T22:34:21.504Z","avatar_url":"https://github.com/gowthamand.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# JavaScript Without jQuery\nTips and practical examples, from the [CatsWhoCode post](http://www.catswhocode.com/blog/javascript-without-jquery-tips-and-practical-examples).\n\n\n## Table of Contents\n\n1. [Listening for Document Ready](#listening-for-document-ready)\n1. [Adding Event Listeners](#adding-event-listeners)\n1. [Selecting Elements](#selecting-elements)\n1. [Using Ajax](#using-ajax)\n1. [Adding and Removing Classes](#adding-and-removing-classes)\n1. [Fade In](#fade-in)\n1. [Hiding and Showing Elements](#hiding-and-showing-elements)\n1. [DOM Manipulation](#dom-manipulation)\n\n\n### Listening for Document Ready\n\nA page can't be manipulated safely until the document is \"ready\". For that reason, we developers have taken the habit to wrap all of our JS code inside the jQuery `$(document).ready()` function:\n\n```javascript\n$(document).ready(function () {\n  console.log('ready!');\n});\n```\n\nThe same result can be achieved easily using pure JavaScript:\n\n```javascript\ndocument.addEventListener('DOMContentLoaded', function () {\n  console.log('ready!');\n});\n```\n\n\n### Adding Event Listeners\n\nEvent Listeners are a very important part of JavaScript development. jQuery has a very easy way to handle them:\n\n```javascript\n$(someElement).on('click', function () {\n  // TODO event handler logic\n});\n```\n\nYou don't need jQuery to create event listeners in JavaScript. Here's how to do so:\n\n```javascript\nsomeElement.addEventListener('click', function () {\n  // TODO event handler logic\n});\n```\n\n\n### Selecting Elements\n\njQuery makes it super easy to select elements using an ID, a class name, tag name, etc:\n\n```javascript\n// By ID\n$('#myElement');\n\n// By Class name\n$('.myElement');\n\n// By tag name\n$('div');\n\n// Children\n$('#myParent').children();\n\n// Complex selecting\n$('article#first p.summary');\n```\n\nPure JavaScript features various way to select elements. All of the methods below work on all modern browsers as well as IE8+.\n\n```javascript\n// By ID\ndocument.querySelector('#myElement');\n\n// By Class name\ndocument.querySelectorAll('.myElement');\n\n// By tag name\ndocument.querySelectorAll('div');\n\n// Children\ndocument.querySelectorAll('myParent').childNodes;\n\n// Complex selecting\ndocument.querySelectorAll('article#first p.summary');\n```\n\n\n### Using Ajax\n\nAs most of you know, Ajax is a set of technologies allowing you to create asynchronymous web applications. jQuery has a set of useful methods for Ajax, including `get()` as shown below:\n\n```javascript\n$.get('ajax/test.html', function (data) {\n  $('.result').html(data);\n  alert('Load was performed.');\n});\n```\n\nAlthough jQuery makes Ajax development easier and faster, it's a sure thing that you don't need the framework to use Ajax:\n\n```javascript\nvar request = new XMLHttpRequest();\nrequest.open('GET', 'ajax/test.html', true);\n\nrequest.onload = function (e) {\n  if (request.readyState === 4) {\n\n    // Check if the get was successful.\n\n    if (request.status === 200) {\n      console.log(request.responseText);\n    } else {\n      console.error(request.statusText);\n    }\n  }\n};\n```\n\n\n### Adding and Removing Classes\n\nIf you need to add or remove an element's class, jQuery has two dedicated methods to do so:\n\n```javascript\n// Adding a class\n$('#foo').addClass('bold');\n\n// Removing a class\n$('#foo').removeClass('bold');\n```\n\nWithout jQuery, adding a class to an element is pretty easy. To remove a class, you'll need to use the `replace()` method:\n\n```javascript\n// Adding a class\ndocument.getElementById('foo').className += 'bold';\n\n// Removing a class\ndocument.getElementById('foo').className = document.getElementById('foo').className.replace(/^bold$/, '');\n```\n\n\n### Fade In\nHere's a practical example of why jQuery is so popular. Fading an element only takes a single line of code:\n\n```javascript\n$(el).fadeIn();\n```\n\nThe exact same effect can be achieved in pure JavaScript, but the code is way longer and more complicated.\n\n```javascript\nfunction fadeIn(el) {\n  el.style.opacity = 0;\n\n  var last = +new Date();\n  var tick = function() {\n    el.style.opacity = +el.style.opacity + (new Date() - last) / 400;\n    last = +new Date();\n\n    if (+el.style.opacity \u003c 1) {\n      (window.requestAnimationFrame \u0026\u0026 requestAnimationFrame(tick)) || setTimeout(tick, 16);\n    }\n  };\n\n  tick();\n}\n\nfadeIn(el);\n```\n\n\n### Hiding and Showing Elements\n\nHiding and showing elements is a very common task. jQuery offers the `hide()` and `show()` methods for modifying the display of an element.\n\n```javascript\n// Hiding element\n$(el).hide();\n\n// Showing element\n$(el).show();\n```\n\nIn pure JavaScript, showing or hiding elements isn't more complicated:\n\n```javascript\n// Hiding element\nel.style.display = 'none';\n\n// Showing element\nel.style.display = 'block';\n```\n\n\n### DOM Manipulation\n\nManipulating the DOM with jQuery is very easy. Let's say you would like to append a `\u003cp\u003e` element to `#container`:\n\n```javascript\n$('#container').append('\u003cp\u003emore content\u003c/p\u003e');\n```\n\nDoing so in pure JavaScript isn't much of a big deal either:\n\n```javascript\ndocument.getElementById('container').innerHTML += '\u003cp\u003emore content\u003c/p\u003e';\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgowthamand%2Fjavascript-without-jquery","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgowthamand%2Fjavascript-without-jquery","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgowthamand%2Fjavascript-without-jquery/lists"}