Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

jquery like selector in javascript

var domElement = function(selector) {
 this.selector = selector || null;
 this.element = null;
};
domElement.prototype.init = function() {
 switch (this.selector[0]) {
 case ‘<’:
 var matches = this.selector.match(/<([w-]*)>/);
 if (matches === null || matches === undefined) {
 throw ‘Invalid Selector / Node’;
 return false;
 }
 var nodeName = matches[0].replace(‘<’, ‘’).replace(‘>’, ‘’);
 this.element = document.createElement(nodeName);
 break;
 default:
 this.element = document.querySelector(this.selector);
 }
};
domElement.prototype.on = function(event, callback) {
 var evt = this.eventHandler.bindEvent(event, callback, this.element);
}
domElement.prototype.off = function(event) {
 var evt = this.eventHandler.unbindEvent(event, this.element);
}
domElement.prototype.val = function(newVal) {
 return (newVal !== undefined ? this.element.value = newVal : this.element.value);
};
domElement.prototype.append = function(html) {
 this.element.innerHTML = this.element.innerHTML + html;
};
domElement.prototype.prepend = function(html) {
 this.element.innerHTML = html + this.element.innerHTML;
};
domElement.prototype.html = function(html) {
 if (html === undefined) {
 return this.element.innerHTML;
 }
 this.element.innerHTML = html;
};
domElement.prototype.eventHandler = {
 events: [],
 bindEvent: function(event, callback, targetElement) {
 this.unbindEvent(event, targetElement);
 targetElement.addEventListener(event, callback, false);
 this.events.push({
 type: event,
 event: callback,
 target: targetElement
 });
 },
 findEvent: function(event) {
 return this.events.filter(function(evt) {
 return (evt.type === event);
 }, event)[0];
 },
 unbindEvent: function(event, targetElement) {
 var foundEvent = this.findEvent(event);
 if (foundEvent !== undefined) {
 targetElement.removeEventListener(event, foundEvent.event, false);
 }
 this.events = this.events.filter(function(evt) {
 return (evt.type !== event);
 }, event);
 }
};
$ = function(selector) {
 var el = new domElement(selector);
 el.init();
 return el;
}
Comment

jquery like selector in javascript

var $ = (function () {

	'use strict';

	/**
	 * Create the constructor
	 * @param {String} selector The selector to use
	 */
	var Constructor = function (selector) {
		if (!selector) return;
		if (selector === 'document') {
			this.elems = [document];
		} else if (selector === 'window') {
			this.elems = [window];
		} else {
			this.elems = document.querySelectorAll(selector);
		}
	};

	/**
	 * Do ajax stuff
	 * @param  {String} url The URL to get
	 */
	Constructor.prototype.ajax = function (url) {
		// Do some XHR/Fetch thing here
		console.log(url);
	};

	/**
	 * Run a callback on each item
	 * @param  {Function} callback The callback function to run
	 */
	Constructor.prototype.each = function (callback) {
		if (!callback || typeof callback !== 'function') return;
		for (var i = 0; i < this.elems.length; i++) {
			callback(this.elems[i], i);
		}
		return this;
	};

	/**
	 * Add a class to elements
	 * @param {String} className The class name
	 */
	Constructor.prototype.addClass = function (className) {
		this.each(function (item) {
			item.classList.add(className);
		});
		return this;
	};

	/**
	 * Remove a class to elements
	 * @param {String} className The class name
	 */
	Constructor.prototype.removeClass = function (className) {
		this.each(function (item) {
			item.classList.remove(className);
		});
		return this;
	};

	/**
	 * Instantiate a new constructor
	 */
	var instantiate = function (selector) {
		return new Constructor(selector);
	};

	/**
	 * Return the constructor instantiation
	 */
	return instantiate;

})();
Comment

PREVIOUS NEXT
Code Example
Javascript :: Cannot GET /assets/vendor/swiper/swiper-bundle.min.js.map 
Javascript :: mongoose fails to connect to server when database is specified 
Javascript :: rotas react com axios 
Javascript :: jquery detach and remove 
Javascript :: recoilOutside npm 
Javascript :: elon musk 4k photo 
Javascript :: where to add const form = document.querySelector(".top-banner form"); form.addEventListener("submit", e = { e.preventDefault(); const inputVal = input.value; }); 
Javascript :: how to embed element in to array 
Javascript :: action cable nuxtjs 
Javascript :: is javascript case sensitive 
Javascript :: google-maps-react give undefined lat long 
Javascript :: storybook absolute paths 
Javascript :: input type disappear side scroll react 
Javascript :: front end display data frmo database nodejs html 
Javascript :: tinymce image and links inputs disabled 
Javascript :: jsetracker 
Javascript :: missing data added between two points javascript 
Javascript :: string and charater alphabet order 
Javascript :: react interactions 
Javascript :: js toggle opacity 
Javascript :: find value number in enzym 
Javascript :: what is steal.js 
Javascript :: remove or replacing element array in javascript 
Javascript :: check textbox value on ng-change value == in angular 
Javascript :: jQuery Validate remote method usage to check if username already exists 
Javascript :: purecomponent re rendering 
Javascript :: image popup js close button 
Javascript :: how to make modules structure like lodash 
Javascript :: how to compare a string with its ending in javascript 
Javascript :: angular file upload app with django 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =