Random color generator in javascript2 min read

Javascrpt color generator

If you ever tried to create HTML or SVG graphs, it is most likely that you have asked google the question How to generate random colors in javascript? , because I for sure did ???? .

Maybe you have searched it for some other reason. To create some cool UI or something, or maybe to irritate the user with stupid colors ???? .

If you have never searched it or thought about it, well why don’t you go ahead and try it now, it’s free after all.

There are plenty of resources in the internet that would help you get the job done. But here is my little experiment.

Where to start?

I want a function that would take the number of colors to generate, as input and outputs the generated colors.

Something like this –

So I decided to start with one color and tweak and shuffle the RGB values to generate random colors.

 The full function

Usage

Gotta better technique (highly likely), comment below  ???? .

See the color generator in action-

See the Pen Javascript random color generation by hrishikesh mk (@avastava) on CodePen.0

Creating a Javascript load more plugin part 2 – Searching2 min read

Javascript load more plugin with search

In the first part Creating a Javascript load more plugin part 1 , we have created a javascript plugin which loads data from an API page. Let’s build upon on it.

Today we are going to add search option to our plugin, children ? .

Objectives ?

  • Add search bar to the page.
  • On input on search bar, load in relevant content from the API.
  • The API should take in a search parameter and provide relevant results.

As of now we are sending two parameters to the API – start and count. We would add one more q for sending the search query.

For learning how add searching option at the server end, please refer this tutorial from Codecourse – Basic PHP & MySQL Searching.

It uses PHP and MYSQL but you get the idea ? .

Demo page – Javascript load more plugin – search added.

We have to add one more line to our previous HTML code to include the search bar.

We can add this anywhere inside our main container. Only condition is that it should have a class of finder, so that our plugin can identify it.

For styling HTML forms and form elements view this tutorial – Styling HTML Forms .

Updating the Plugin

Now let’s upgrade our plugin. First we would find the search bar.

If you are not able to follow, please go through part one of this series.

Now, inside our lmore.load method we would append the search term to the GET query string.

Let’s add and event listener to the search bar so that, data can be loaded while searching.

We could load data on any event, but I prefer the on-input event because it gives an instant search effect.

I don’t like search bars that have a button which has to be clicked to search. We are in 2016, common guys ( who make me click a button to search ? )  ?.

We could even pass the event, as input for the plugin.

You could also use this plugin to give search suggestions like google. Maybe we could use the plugin two times, one for suggestions and one for showing search results.

Comment below if you want to see this plugin give you search suggestions.

Javascript load more plugin6 min read

Javascript load more plugin

When searching the internet for a Javascript load more plugin, I came across a Jquery load more plugin tutorial in this youtube channel – Codecourse.

I highly recommend that the reader watch the Codecourse (formerly PHPacademy) tutorial and subscribe to their channel before continuing.

Link to the tutorial – Load More jQuery Plugin.

The tutorial was a really good and the plugin was very useful. But, with the aim of getting better at javascript and programming in general I tried to customize it.

So I decided to redo it in pure javascript. Here is how it went.

Demo link – Javascript load more plugin demo.

Please refer the above tutorial for creating a server side JSON API. The API would look like this Load more JSON API.

Now let’s get started with our plugin.

How to use?

Parameters

  • elem: The main container.
  • source: JSON API page.
  • count: Number of results to load per request.

HTML

The plugin requires an HTML template to work with and the template would look like this.

Elements included are

  • #cont – Main Container.
  • .rescont – Container where results have to be loaded.
  • .items – Container for .item
  • .item – Individual data container which would be cloned with each result set.
  • [data-field] – The value of this attribute would be matched with the keys in the JSON object and corresponding values would be filled.
  • .nores – The element to be shown when there are no results. .rescont would be hidden and .nores would be shown if there are no results.
  • .load_more – Button which on click would load more results.

JAVASCRIPT

The plugin would take 3 inputs as given in the usage section.

It would have two main functions

  1. load – A function to load data from the JSON API page using ajax.
  2. append – A function to append the loaded data to the DOM.

The plugin

Wrapper

Now we have to set the necessary properties.

Getting load more button and saving it and it’s display property

Cloning .item element and removing it from the DOM.

The load function

The append function

Function to set value of any element

Initial loading

Also setting event listener on load more button to load data on click

And finally the plugin is complete ????.

Download link – lmore.js.

Leave your suggestions below.