JS Highlight Text

I needed to setup some text search functionality on a page and the client really wanted the searched words to be highlighted.

I found a great library for exactly this over on The Art of the Web.

They do a great job of explaining this library so please check them out to learn more!

Hash Link Offset

When you have a hash link on your page ( href=”#target” ) sometimes the browser scrolls too low and cuts off or puts too close to the top the target element.

You can offset this hash link by hooking into the ‘hashchange’ event.

This does NOT resolve linking directly to this hash tag, but will fix the on page hash link.

To do that you can setup the following functions.

Thanks to Eric Olsen over at stack overflow!

Google Font Print Issues, font-display: swap!

I had an issue that was driving me up a wall. Certain fonts, technically only the normal font weight of the Open Sans font, was not printing on the first “print” attempt. After clicking print a second time the page text would print as normal.

This issue was due to how Chrome loads / displays fonts. Feel free to read up on ‘font-display’ and Chromes issue with printing web loaded fonts. The short answer is basically Chrome was trying to reload the font for the print page instead of using the font it used on the screen.

The solution is thankfully VERY simple. Just add “&display=swap” to your Google Font url string in order to avoid this issue. This also has a variety of other speed benefits, so you should ALWAYS be adding display=swap to your Google Web fonts and Google Fonts now supports this by default.

Upload Images Using JS

I recently needed to update a form to update through AJAX so the page didn’t reload when submitted. The issue was that we had some images on this form. In the past I have used things like plupload, but that didn’t seem like the right approach in this instance.

At first I looked into the “fetch” api and saw a good post from Tania Rascia, but unfortunately the fetch API still isn’t fully adopted…DAMN YOU MICROSOFT!

Thankfully I was able to find a jQuery version of this same idea over on Makeitweb.com by Yogesh Singh.

I also needed to make sure I sent along post data with the FormData to update the rest of the form. Here is an example of the JS code I ended up using:

On the server script you can access this data just like any other form submission.

PHP

There we go! Now we can submit our form, including images, to our server script!

NOTE: We are using a custom function to serialize our form data. You can find that function here.

Cross Browser HTML5 Date Input

I have been waiting for the day when using <input type=”date” /> would be common…alas, 3 years after the standards were written it is still not supported in IE or Safari.

UNFORTUNATELY we still have to support these browsers…

Traditionally I have been using a third party datepicker (jQuery UI, Bootstrap Datepicker, etc) but I REALLY wanted to just use the default type=”date” this time.

I found a great solution over on Stack Overflow from Kashif Latif.

BAM! We have a polyfill. As far as I can tell this does the trick.

A Better GForms Repeater Example

The Gravity Forms repeater field example is helpful, but has multiple issues that don’t make themselves apparent until you try to implement it.

Thankfully Giorgos Sarigiannidis has a better repeater example that solves some of the most troubling issues of the repeater field.

Here is my example of the same idea, but using a different form as the fields in the repeater. This allows the client to update the fields in the repeater themselves.

 

Gforms doesn’t have a counter system built in yet so if you want to use a counter with your repeater ( ex: Attendee #1, Attendee #2, etc… ) here is some SCSS code you can use to accomplish that.

This will look something like this: