jQuery CSS Class playground

Today we will play with jQuery CSS class methods such as addClassremoveClasshasClass, and toggleClass and learn some basic implementation to modify the HTML styles on the fly.

jQuery CSS Class Methods:

Let’s understand this is a small piece of code. First things first, You must include the jQuery library in the <head>…</head>  tag.

<script src=PATH_TO_JQUERY/jquery.min.js></script>

Now our page has the jQuery library so let’s add some HTML and CSS to play with the jQuery CSS Class methods.

HTML Code:

<div id="elem">
  This is a content panel
</div>

<p>
  <a href="#" >addClass</a> /
  <a href="#" >removeClass</a> /
  <a href="#" >toggleClass</a> /
  <a href="#" >hasClass</a>
</p>

Here we have a DIV tag with ID set to element and we will use this ID to play with jQuery CSS class methods. Below that, we have a paragraph element and four links with different class names. Let’s write some CSS code and I will explain the functionality of each link when we add jQuery code.

 CSS Code

#elem{
	width: 400px;
	text-align: center;
	padding: 20px 0;
	background: #ffffff;
	border: 1px solid #ddd;
	margin: 100px auto 30px auto;
}

.hover{
	background: #cc0000 !important;
	color: #fff !important;
	border: 1px solid #990000 !important;
}

In the CSS styles, I’ve added some styles to our DIV#elem element and also created a class named hover which will change the background, color, and border of the DIV#elem.

Now let’s add some jQuery code and learn how these jQuery CSS methods work.

$(document).ready(function(){
	// Add the class
	$('a.add-class').click(function(){
		$("#elem").addClass('hover');
		$("#elem").html('Class added ".hover"');
		return false;
	});

	// Remove the class
	$('a.remove-class').click(function(){
		$("#elem").removeClass('hover');
		$("#elem").html('Class removed ".hover"');
		return false;
	});

	// Toggle the class and check if the class has been already added or not
	$('a.toggle-class').click(function(){

		$("#elem").toggleClass('hover');

		if($("#elem").hasClass('hover')){
			$("#elem").html('Class added ".hover"');
		}else{
			$("#elem").html('Class removed ".hover"');
		}

		return false;
	});

	// Checks if the class has been already added or not
	$('a.has-class').click(function(){

		if($("#elem").hasClass('hover')){
			$("#elem").html('Class found ".hover"');
		}else{
			$("#elem").html('Class not found ".hover"');
		}

		return false;
	});

});

In the JQuery code, I’ve used the click trigger for all four links we added in the HTML code above. Also, I’ve used return false;  so when we click on the link it should not redirect the user to any page or use # in the current page URL. Also, I’ve added another line of code to modify the HTML of the #elem element when we click on these links. Don’t get confused with the .html() method, this is not required, I just added it to explain the functionality.

The first link with class .add-class will add the .hover class to the #elem element. If you want to add multiple classes with one trigger you can specify different class names separated with a space as we normally do in normal HTML code. Once we click on this link, .hover class will be added to the #elem HTML element and the inner HTML will be changed and show us the result. You can click on the address link above and see it in action.

The second link with class .remove-class will remove the .hover class from the #elem element. If the element doesn’t have a .hover class, it won’t do anything so the remove class will be used only to remove any applied class.

The third link with class .toggleClass will toggle the .hove class on #elem element. If the element does not have a .hover class it will add the same otherwise vice versa. In this code, I’ve used the jQuery CSS method hasClass to check if the element has the .hover class or not. and the content of the #elem element is changed accordingly.

The fourth link will only check if the class .hover is applied to the #elem element or not and change the HTML code of the #elem element accordingly.

I hope this will clarify any doubts on using these methods, in case you have any questions or suggestions, please use the comments below to start a conversation on this topic.

contact us

Web development Services

CSS3 Animation – Stylish Links and other HTML elements

Links, Links, Links!! That’s what we have all over the internet. On our website and blog posts, we have a lot of links to point a user to another page. Today I will share a small trick to stylize the links with CSS3 Animation. That’s what I’ve used on this website and you can apply the same as well to change the link color with style on hover.

CSS3 Animation

With CSS3 animation, we can add some effects when changing from one style to another without using any flash or javascript. We will use CSS animation transition to add some cool effects to the anchor tags.

How it works

CSS3 transition effects let an element gradually change from one style to another. To do this we must specify two things:

Specify the CSS property we want to add an effect too.
Specify the duration of the effect.
Optionally we can specify “transition-timing-function” which is set to “ease” by default however, we can use any of these.

 linear|ease|ease-in|ease-out|ease-in-out

Now let us play with CSS3 Animation transition

HTML Code

<a href="#" title="" > CSS3 Animation - Color </a>
<a href="#" title="" > CSS3 Animation - Background Color </a>
<div > Change Width </div>

CSS Code

.color-animation{
	color: blue;
	-webkit-transition: color 1s ease;
	-moz-transition: color 1s ease;
	-o-transition: color 1s ease;
	-ms-transition: color 1s ease;
	transition: color 1s ease;
}
.color-animation:hover{
	color: red;
	-webkit-transition: color 1s ease;
	-moz-transition: color 1s ease;
	-o-transition: color 1s ease;
	-ms-transition: color 1s ease;
	transition: color 1s ease;
}
.background-animation{
	background: blue !important;
	color: white !important;
	-webkit-transition: background 1s ease;
	-moz-transition: background 1s ease;
	-o-transition: background 1s ease;
	-ms-transition: background 1s ease;
	transition: background 1s ease;
}
.background-animation:hover{
	background: red !important;
	color: white !important;
	-webkit-transition: background 1s ease;
	-moz-transition: background 1s ease;
	-o-transition: background 1s ease;
	-ms-transition: background 1s ease;
	transition: background 1s ease;
}
.change-width{
	width: 150px;
	background: #444;
	color: #fff;
	margin: 20px auto;
	padding: 10px;
	-webkit-transition: width 1s ease;
	-moz-transition: width 1s ease;
	-o-transition: width 1s ease;
	-ms-transition: width 1s ease;
	transition: width 1s ease;
}
.change-width:hover{
	width: 350px;
	-webkit-transition: width 1s ease;
	-moz-transition: width 1s ease;
	-o-transition: width 1s ease;
	-ms-transition: width 1s ease;
	transition: width 1s ease;
}

Try this code on a blank HTML page and add the CSS either within ... tags or in your stylesheet. Play around a bit with the CSS to understand the CSS3 Animation transition and create some cool effects for your website links and other block-level elements. Feel free to discuss this further in the comments. You can also contact IOGOOS Solution to build Website Animation Services.

WordPress Developer

Website Development Services

Google Web Fonts – How to Use, Download, and Sync

Typography is the key element in the design process. While designing a web page, business card, brochure or newspaper advertisement fonts and text style plays an important role to get user attention. To design stunning elements we need fonts and fortunately, the big daddy provides a lot of them for free. We can use Google Web Fonts in our designs and they are pretty awesome and very easy to use.

How to use Google Web Fonts on a web page

It’s pretty easy to use Google Web Fonts on your website. You can do that in just three simple steps:

  1. Choose single or multiple Google Web Font(s) from Google fonts website: //www.google.com/fonts and add them in your collection.
  2. Hit the use link above the list of your selected fonts and Google will show you different methods to include these fonts on your website. Standard, @import or Javascript
  3. I always use Standard method with a small trick to load the fonts before displaying the web page.

Here’s the code you would need to use Google Web Fonts on your website.

I’ve added Open Sans and Lobster fonts to my collection for this example.

<head>
<link href='//fonts.googleapis.com/css?family=Lobster|Open+Sans' rel='stylesheet' type='text/css'>
<!-- This will prefetch content from google web fonts website -->
<link rel="dns-prefetch" href="//fonts.googleapis.com" />
</head>

The above code goes within the [htmltag tag=”code”]<head>…</head>[/htmltag] tags and the DNS-prefetch link tag will fetch the contents from Google Web Fonts website. This will help resolve issues like font changes after the page load.

body{
    font-family: 'Open Sans', sans-serif;
}
h1, h2, h3, h4, h5, h6{
    font-family: 'Lobster', cursive;
}

In the above CSS code, I’ve defined the body font as open sans and headings font as lobster. Similarly, you can use different fonts for different elements.

How to Use Google Web Fonts while Designing?

As a designer, we all need to design the mockups of the creative work before we can publish the same. If we need to use Google Web Fonts while designing layouts in Photoshop or Illustrator we will have to download the font and install the same in our Operating System.

We can always click the Download link to save the fonts on our hard drive and then install the font on our operating system. However, what will happen if that font is updated and new characters are added or there’s a new and improved version available for that font. We need to keep a check and if the new version is available, we need to uninstall the previous version and then install the new version.

What is there’s some tool which can automatically do this task and save us a lot of time and effort?

Fortunately, there are tools available that can make our life easy and keep Google Web Fonts updated all the time.

Using Skyfonts to Download and Sync Google Fonts

Fonts.com has teamed up with Google to offer google web fonts for desktops via Skyfonts software.

We are proud to have teamed up with Google to offer desktop versions of their popular Google Fonts free of charge. Offered for use in print, these fonts are delivered using SkyFont’s patent-pending font delivery technology and can be used anywhere.

Each time a font is updated — such as when new characters are added — SkyFonts will automatically update the font on your device. Syncing Google Fonts with SkyFonts will also improve your web browsing experience, by cutting the time spent downloading fonts. ~ Fonts.com

Skyfonts software is available for both Mac and Windows. You can choose the fonts you want to use while designing and Skyfonts sync these fonts with your operating system fonts and make them available in all applications as normally installed fonts. If a new version of the installed font is available, the Skyfonts application will automatically sync and replace the older one with the new version.

Download Skyfonts Software

There are other methods available to download all Google Web Fonts on your computer, however, I found Skyfonts the easiest and most efficient to keep all the fonts updated.

Signup.

Add Custom Post Types to Main WordPress RSS Feed

Custom post types are a great feature introduced in WordPress 3.0 and it extended the functionality and practical usage of this awesome CMS. While re-designing / re-structuring iogoos.com, I have used Custom Post Types for different sections on this website.

Now I want to add all these custom post types to the main WordPress feed so the users get updated content from each section via RSS. There are two ways we can do that. You can add the below code snippets to the functions.php file of your theme.

1. Add all Custom Post Types to Main WordPress RSS Feed

function extend_feed($qv) {
	if (isset($qv['feed']))
		$qv['post_type'] = get_post_types();
	return $qv;
}
add_filter('request', 'extend_feed');

Function get_post_types() will return all registered custom post types. Here you can learn more about get_post_types() function.

2. Add selected Custom Post Types to Main WordPress RSS Feed

I’ve used the woo-commerce plugin for the shop section of this website and this plugin added a few more custom post types that may be not relevant or useful for my readers. So with the code snippet below, we can add a few selected Custom Post Types to the main WordPress RSS Feed.

function extend_feed($qv) {
	if (isset($qv['feed']) && !isset($qv['post_type']))
		$qv['post_type'] = array('post', 'tutorials', 'tips', 'snippets', 'shop');
	return $qv;
}
add_filter('request', 'extend_feed');

If you notice, here instead of using the get_post_types() function, I supplied an array of specific custom post types slugs. This will add content to WordPress’s main RSS feed only from the specified post types.

Contact Us
iogoos.com

previous article

How to create Quick Search Bookmarks with JS

As a web designer, I always look for design elements for inspiration. So to search for images normally open Google.com, Flickr.com, and other preferred sites, apply different search filters and type keywords and then hit enter to view the results. It’s ok to follow this process if we need to do this two or three times a day. But for a designer, it’s not the case. We look for resources a lot and that wastes a lot of our valuable time.

What if we can automate this task and reduce a few steps? I created javascript bookmarks for different searches and it’s helping me a lot to save time and search for resources with only a click, type keywords, and hit enter.

Let’s create a bookmark to search google with an image size larger than 1024×768 resolution with a license filter labeled for reuse.

Step 1: Create search query

Open Google.com and search for “Abstract Wallpapers”.
This will show the result of the web search. Click Images to view image search results.

Step 2: Apply search filters

Now on this page, we can click on search tools and apply a few filters to our search results. You can apply whatever you want, however for this trick, I am setting Size to Larger than 1024×768 and Usage rights to Labeled for reuse.

Step 3: Get the query URL

By this time, the URL in the address bar has changed with specific query parameters we applied in the above steps. Copy the URL in the address bar and save it in a text or any editor you use to write Javascript.

Step 4: Write Javascript Code

The string we copied might be a very long string but we need to look for the keyword we used to search in step 1. In this case, it is the Abstract Wallpapers. The URL will have this value encoded which is something like “abstract+wallpaper”. So if you search for abstract, you will see the query param it issuing. In this case, is q=keyword

function(){
	var keyword = prompt("Type keywords:");
	var url = '//www.google.com/search?q='+keyword+'&hl=en&authuser=0&source=lnms&tbm=isch&sa=X&ei=q-QNU-LgIoSCrAezzoDIDw&ved=0CAcQ_AUoAQ&biw=1920&bih=1079#authuser=0&hl=en&q=abstract+wallpaper&tbm=isch&tbs=isz:lt,islt:xga,sur:fc'; 
	var win = window.open(url, '_blank'); 
	win.focus();
}

In the above code, line 1, is to start a javascript function, and line 6 is to wrap the function code.

Line 2, we define a Javascript prompt for the keyword to search.

Line 3, we define the URL we copied however, there’s one change we need to do. We need to pass the keyword variable to the query parameter so, remove everything after q= till & and pass the keyword variable instead. For this, we use the Javascript concatenation technique.

'string'+var+'string';

Make sure you use the same quotes for concatenation if your declaration is within single quotes use single otherwise double. If this isn’t right, the function will break.

Line 4, We use the javascript technique to open a tab or new page in the browser and pass the URL parameter with _blank for a new window or tab.

Line 5, We tell the program to focus on the new window.

Step 5: Creating the bookmark

Now our function is ready and we need to create a bookmark. Carefully bring all the code in one line and wrap it between
javascript:(FUNCTION_IN_ONE_LINE)();

The end result should be like this:

javascript:(function(){var keyword = prompt("Type keywords:"); var url = '//www.google.com/search?q='+keyword+'&source=lnms&tbm=isch&sa=X&ei=zzsGU5_mIouyrgf9uIDYDA&ved=0CAgQ_AUoAg&biw=1918&bih=1079#q='+keyword+'&tbm=isch&tbs=isz:lt,islt:2mp,sur:fc'; var win = window.open(url, '_blank'); win.focus();})();

Step 6: Test and create a bookmark

To test this, you can simply copy the entire line and paste it into the browser address bar. It should ask for the keyword. Once you type the keyword and hit enter, it should open the Google image search results with applied filters.

Once our test is passed, Right-click anywhere on the bookmark bar and click add new. Type a title and enter the code we created in the URL field. Save it.

Now you have a bookmark to search for images greater than 1024×768 size with a license to reuse.

You can create these bookmarks for any website, just change the URL and the keyword variable where it is required. Here are a few from my collection:

Google Image Search

javascript:(function(){var keyword = prompt("Type keywords:"); var url = '//www.google.com/search?q='+keyword+'&source=lnms&tbm=isch&sa=X&ei=zzsGU5_mIouyrgf9uIDYDA&ved=0CAgQ_AUoAg&biw=1918&bih=1079#q='+keyword+'&tbm=isch&tbs=isz:lt,islt:2mp,sur:fc'; var win= window.open(url, '_blank'); win.focus();})();

Flickr Image Search

javascript:(function(){var keyword = prompt("Type keywords:"); var url = '//www.flickr.com/search/?q='+keyword+'&m=tags&l=deriv&ss=0&ct=5&mt=photos&w=all&adv=1'; var win= window.open(url, '_blank'); win.focus();})();

Icon Search @ Iconfinder.com

javascript:(function(){var keyword = prompt("Type keywords:"); var url = '//www.iconfinder.com/search/?q='+keyword+'&maximum=512&price=free'; var win= window.open(url, '_blank'); win.focus();})();

Dribble Search

javascript:(function(){var keyword = prompt("Type keywords:"); var url = '//dribbble.com/search?q='+keyword; var win= window.open(url, '_blank'); win.focus();})();

Go ahead, spend some time to create these quick and easy bookmarks, and save huge time in the future. If you like this trick, please share the link with your friends.

contact us

WEb Development Services

How to create a category-based search box with CSS and jQuery

There are a few times when we want our website visitors to search content in just one category and not the whole website. Today I am sharing a simple but effective UI trick to create a category-based search box with CSS and jQuery.

HTML

<div >
  <input type=text value="" placeholder="search:" />
  <div >
   <label><input type=radio name=filter value="value" /> Category One</label>
   <label><input type=radio name=filter value="value" /> Category Two</label>
   <label><input type=radio name=filter value="value" /> Category Three</label>
   <label><input type=radio name=filter value="value" /> Category Four</label>
   <label><input type=radio name=filter value="value" /> Category Five</label>
   <label><input type=radio name=filter value="value" /> Category Six</label>
  </div>
</div>

Now let’s just add some styles to out HTML code above.

CSS

#demo {
  width: 600px;
  margin: 100px auto 0 auto;
}
#demo .search-box {
  width: 100%;
  position: relative;
}
#demo .search-box input[type="text"] {
  width: 100%;
  padding: 10px;
  background: #fff;
  border: 1px solid #ddd;
  font-size: 12pt;
  margin: 0px;
}
#demo .search-box input[type="text"]:focus {
  box-shadow: none !important;
  outline: none !important;
}
#demo .search-box .search-filters {
  display: none;
  width: 100%;
  background: #fff;
  padding: 10px;
  border: 1px solid #ddd;
  border-top: 0px;
}
#demo .search-box .search-filters:after {
  content: "";
  display: table;
}
#demo .search-box .search-filters label {
  margin-bottom: 7px;
  font-size: 13px;
  display: inline-block;
  width: 50%;
  float: left;
}

You can use the above CSS or create your own styles by changing the backgrounds, colors etc.

Now let’s add the jQuery magic and make this piece of code look great

jQuery

Make sure you call the jQuery script on the page by adding this code on the page.

<img src=data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7 data-wp-preserve="%3Cscript%20src%3D%22http%3A%2F%2Fcode.jquery.com%2Fjquery-1.11.0.min.js%22%3E%3C%2Fscript%3E" data-mce-resize="false" data-mce-placeholder="1" width=20 height=20 alt="<script>" title="<script>" />
<img src=data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7 data-wp-preserve="%3Cscript%20src%3D%22http%3A%2F%2Fcode.jquery.com%2Fjquery-migrate-1.2.1.min.js%22%3E%3C%2Fscript%3E" data-mce-resize="false" data-mce-placeholder="1" width=20 height=20 alt="<script>" title="<script>" /><span id="mce_marker" data-mce-type="bookmark" data-mce-fragment="1">​</span>
jQuery(document).ready(function($){
  $('.search-box input[type="text"]').focus(function(){
    $('.search-filters').slideToggle();
  });
  $('.search-filters input[type="radio"]').on('click', function(){
    var placeholder_text = $(this).closest('label').text();
    $('.search-input').attr('placeholder', 'search: '+placeholder_text);
    $('.search-filters').slideToggle();
  });
});<span id="mce_marker" data-mce-type="bookmark" data-mce-fragment="1">​</span>
contact us

Like this code.

Our Services

Change WordPress Excerpt Length

You can add the following code in your theme’s functions.php file to change the WordPress excerpt length. Change the number (100) to the desired length.

add_filter('excerpt_length', 'new_excerpt_length');
function new_excerpt_length($length) { return 100; }
contact us

IOGOOS WordPress development services

How-To: Copy current directory path in Terminal

While working on multiple projects I always need to copy the current path of a directory in Terminal. I used to use the command pwd in the terminal that prints the current directory path in the terminal and then I have to select the path with the mouse to copy and paste the same where I needed.

I hate to use the mouse as it wastes a lot of time and to save time and copy the current path without selecting it with the mouse, I found this command and use it all the time.

pwd | pbcopy

This command will copy the current directory path to the clipboard and we can then press CMD+V (CTRL+V for Windows) to paste the path wherever needed.

I hope this will help someone save time and be more productive.+

img

Our Services

How-to: Get Current Url in PHP with or without Query String

In each PHP website, we need to find out the current URL of the page, and here’s the PHP code to find out the current URL of the page a user is browsing with or without the query string.

function currentUrl( $trim_query_string = false ) {
    $pageURL = (isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] == 'on') ? "//" : "//";
    $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
    if( ! $trim_query_string ) {
        return $pageURL;
    } else {
        $url = explode( '?', $pageURL );
        return $url[0];
    }
}

I like to keep such functions in a helpers class so I can use them anywhere in the app. To use it in a PHP class you can use the following code:

public function currentUrl( $trim_query_string = false ) {
    $pageURL = (isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] == 'on') ? "//" : "//";
    $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
    if( ! $trim_query_string ) {
        return $pageURL;
    } else {
        $url = explode( '?', $pageURL );
        return $url[0];
    }
}
Contact Us

Our Services

How-To: Create Easy Element Toggles with Data Attributes and jQuery.

While working on my latest WordPress plugin, I found myself using jQuery toggleClass on multiple elements and writing JS code for each was not at all a better solution. To fix this issue and keep the code clean and short I used the following code.

$(document).on('click', '[data-toggle="class"]', function () {
    var $target = $($(this).data('target'));
    var classes = $(this).data('classes');
    $target.toggleClass(classes);
    return false;
});

Make sure you have jQuery script on your page and add this code to your Javascript file and then you can use it in your HTML code as below:

<a href="#" data-toggle="class" data-target="#element" data-classes="is-active">Click here</a> to toggle.
<div id="element">...content goes here...</div>

The anchor tag with data-toggle will look for an element with ID or element and toggle is-active class for that element. You can apply this logic to any UI element such as Modal with an overlay background and use the data-toggle attributes on the modal background as well to hide the modal.

Feel free to implement and extend this code on your projects.

contact us

Our services

Enquiry Now

When We Work Together

We can create something incredible

arrow
HQ INDIA
HQ INDIA
C-31, Milap Nagar,
Uttam Nagar, New Delhi,
Delhi 110059
USA
USA
6715 Backlick Rd Suite 202
Springfield,
VA 22150, USA
AUSTRALIA
AUSTRALIA
2/51, Lane Cres,
Reservoir, VIC
3037, Australia
CANADA
CANADA
61 Payzant Bog Road, Falmouth, NS, B0P 1P0, CANADA
UK
UK
3rd Floor, 131 City Road, London, EC1V 2NX, United Kingdom
UAE
UAE
Boutik Mall, Al Reem Island - Abu Dhabi, UAE
X

Let Us Call You Back

  • India+91
  • United States+1
  • United Arab Emirates+971

Your phone number is kept confidential
and not shared with others.