All Projects → AllThingsSmitty → javascript-without-jquery

AllThingsSmitty / javascript-without-jquery

Licence: other
Tips and practical examples

Projects that are alternatives of or similar to javascript-without-jquery

normas
Normal Lightweight Javascript Framework for server-side render compatible with Turbolinks
Stars: ✭ 32 (-31.91%)
Mutual labels:  event-listener, dom-manipulation
m4q
Small library for DOM manipulation and animation. This library similar to jQuery, but written more simply.
Stars: ✭ 30 (-36.17%)
Mutual labels:  ajax, dom-manipulation
cqrs
A foundational package for Command Query Responsibility Segregation (CQRS) compatible with Laravel.
Stars: ✭ 37 (-21.28%)
Mutual labels:  event-listener
Ajaxinate
🎡 Ajax pagination plugin for Shopify themes
Stars: ✭ 107 (+127.66%)
Mutual labels:  ajax
micro-typed-events
The smallest, most convenient typesafe TS event emitter you'll ever need
Stars: ✭ 39 (-17.02%)
Mutual labels:  event-listener
transceiver
Channel based event bus with request/reply pattern, using promises. For node & browser.
Stars: ✭ 25 (-46.81%)
Mutual labels:  event-listener
WebDAVAjax
WebDAV AJAX Library for opening docs from a web page and saving back directly to server in a SharePoint-like manner.
Stars: ✭ 16 (-65.96%)
Mutual labels:  ajax
orchestrate-node
This Orchestrate library provides convenient access to the Orchestrate API from applications written in server-side NodeJS
Stars: ✭ 19 (-59.57%)
Mutual labels:  event-listener
musicWebTemplate
Free website template built for musicians / artists to promote their music and connect to their audience.
Stars: ✭ 26 (-44.68%)
Mutual labels:  ajax
Event Dispatcher
The EventDispatcher component provides tools that allow your application components to communicate with each other by dispatching events and listening to them.
Stars: ✭ 7,946 (+16806.38%)
Mutual labels:  event-listener
EMAN
一个基于SSM框架与物品的协同过滤算法(ItemCF)的简单电子书推荐系统
Stars: ✭ 48 (+2.13%)
Mutual labels:  ajax
Mitt
🥊 Tiny 200 byte functional event emitter / pubsub.
Stars: ✭ 6,945 (+14676.6%)
Mutual labels:  event-listener
three-onEvent
Add an EventListener for Object3d in your three.js project.(support click,hover or gaze)
Stars: ✭ 55 (+17.02%)
Mutual labels:  event-listener
demo-chatroom
go+iris+jwt+mysql+xorm+viper,iris项目实战简易聊天室,登录、注册、私聊、群聊。
Stars: ✭ 47 (+0%)
Mutual labels:  ajax
tsee
Typed EventEmitter implemented with tsargs
Stars: ✭ 22 (-53.19%)
Mutual labels:  event-listener
miniAjax
🚀A mini Ajax library provides Ajax, jsonp and ready features for simple web applications.
Stars: ✭ 67 (+42.55%)
Mutual labels:  ajax
anim-event
Event Manager for Animation
Stars: ✭ 25 (-46.81%)
Mutual labels:  event-listener
ng-event-options
Enable event options (capture, passive, ...) inside angular templates
Stars: ✭ 15 (-68.09%)
Mutual labels:  event-listener
Job-Portal-Django
DJobPortal is a job posting site developed in Django. Where Employer can Register their Company profile, Login Then add Job Post. Employee can bookmark & apply for the Job. There is a dashboard section where Employer can check his job posting list & applicants details also can delete and update his job post. Employee can see his job bookmark lis…
Stars: ✭ 136 (+189.36%)
Mutual labels:  ajax
necktie
Necktie – a simple DOM binding tool
Stars: ✭ 43 (-8.51%)
Mutual labels:  dom-manipulation

JavaScript Without jQuery

Tips and practical examples, from the CatsWhoCode post.

Table of Contents

  1. Listening for Document Ready
  2. Adding Event Listeners
  3. Selecting Elements
  4. Using Ajax
  5. Adding and Removing Classes
  6. Fade In
  7. Hiding and Showing Elements
  8. DOM Manipulation

Listening for Document Ready

A 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:

$(document).ready(function () {
  console.log('ready!');
});

The same result can be achieved easily using pure JavaScript:

document.addEventListener('DOMContentLoaded', function () {
  console.log('ready!');
});

Adding Event Listeners

Event Listeners are a very important part of JavaScript development. jQuery has a very easy way to handle them:

$(someElement).on('click', function () {
  // TODO event handler logic
});

You don't need jQuery to create event listeners in JavaScript. Here's how to do so:

someElement.addEventListener('click', function () {
  // TODO event handler logic
});

Selecting Elements

jQuery makes it super easy to select elements using an ID, a class name, tag name, etc:

// By ID
$('#myElement');

// By Class name
$('.myElement');

// By tag name
$('div');

// Children
$('#myParent').children();

// Complex selecting
$('article#first p.summary');

Pure JavaScript features various way to select elements. All of the methods below work on all modern browsers as well as IE8+.

// By ID
document.querySelector('#myElement');

// By Class name
document.querySelectorAll('.myElement');

// By tag name
document.querySelectorAll('div');

// Children
document.querySelectorAll('myParent').childNodes;

// Complex selecting
document.querySelectorAll('article#first p.summary');

Using Ajax

As 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:

$.get('ajax/test.html', function (data) {
  $('.result').html(data);
  alert('Load was performed.');
});

Although jQuery makes Ajax development easier and faster, it's a sure thing that you don't need the framework to use Ajax:

var request = new XMLHttpRequest();
request.open('GET', 'ajax/test.html', true);

request.onload = function (e) {
  if (request.readyState === 4) {

    // Check if the get was successful.

    if (request.status === 200) {
      console.log(request.responseText);
    } else {
      console.error(request.statusText);
    }
  }
};

Adding and Removing Classes

If you need to add or remove an element's class, jQuery has two dedicated methods to do so:

// Adding a class
$('#foo').addClass('bold');

// Removing a class
$('#foo').removeClass('bold');

Without jQuery, adding a class to an element is pretty easy. To remove a class, you'll need to use the replace() method:

// Adding a class
document.getElementById('foo').className += 'bold';

// Removing a class
document.getElementById('foo').className = document.getElementById('foo').className.replace(/^bold$/, '');

Fade In

Here's a practical example of why jQuery is so popular. Fading an element only takes a single line of code:

$(el).fadeIn();

The exact same effect can be achieved in pure JavaScript, but the code is way longer and more complicated.

function fadeIn(el) {
  el.style.opacity = 0;

  var last = +new Date();
  var tick = function() {
    el.style.opacity = +el.style.opacity + (new Date() - last) / 400;
    last = +new Date();

    if (+el.style.opacity < 1) {
      (window.requestAnimationFrame && requestAnimationFrame(tick)) || setTimeout(tick, 16);
    }
  };

  tick();
}

fadeIn(el);

Hiding and Showing Elements

Hiding and showing elements is a very common task. jQuery offers the hide() and show() methods for modifying the display of an element.

// Hiding element
$(el).hide();

// Showing element
$(el).show();

In pure JavaScript, showing or hiding elements isn't more complicated:

// Hiding element
el.style.display = 'none';

// Showing element
el.style.display = 'block';

DOM Manipulation

Manipulating the DOM with jQuery is very easy. Let's say you would like to append a <p> element to #container:

$('#container').append('<p>more content</p>');

Doing so in pure JavaScript isn't much of a big deal either:

document.getElementById('container').innerHTML += '<p>more content</p>';
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].