This site is my repository for code samples. It is a means for me to document solutions to common problems so that I can reference them quickly when needed, both at work and at home. Samples will be added to this site over the next year or two as I slowly migrate them from (1,074) text files that have served as my repository for over a decade. (I don’t want to do a mass migration as I want each sample to be properly reviewed, formatted, categorized, and tagged. I try to migrate samples as I reference them, time permitting.)

Some samples are complex, some are simple. Some are correct, and some are incorrect. While my intention is that I will be the only user of this site (I’m not an expert, and I don’t expect my samples to solve your code problems!), I don’t see a reason to prevent this content from being available to the general public. Just be warned that this content is not being shared as “the right way” to do things.

That being said, if you do happen to see glaring errors, or have suggestions on how something could be done better, feel free to contact me.

jQuery

Check If Element Has ID Using jQuery

February 11th, 2019|

To check if an element has an ID: if ($(this).is('[id]') { // Has ID console.log('Has ID: ' + $(this).attr('id')); } else { // Has no ID console.log('Has no ID.'); }

jQuery – Selector Starts-Ends With

August 6th, 2018|

Selector Starts With: $('input[name^=somePrefix]').doSomething(); Selector ENDS with: $('input[name$=someSuffix]').doSomething(); In ASP.Net: This is particularly useful in ASP.Net when you may not want to specify clientidmode static, but still want to use jQuery to find the ID. [...]

jQuery – Get Ajax Post Error Details

August 6th, 2018|

$.post('status.ajax.php', {deviceId: id}) .done( function(msg) { ... } ) .fail( function(xhr, textStatus, errorThrown) { alert(xhr.responseText); }); http://stackoverflow.com/a/12116790/4669143

ASP.Net

Post Guidelines (WIP)

April 8th, 2024|

Heads Up: This document is in the process of being updated after moving from Google Prettify to highlight.js. It may take a few days. - Ben, April 8, 2024 Background This site uses Markdown for [...]

Memoize React Components to Prevent Unnecessary Renders

March 18th, 2024|

When the state of a React component changes, the component and all of its children will re-render. While this is fine for most cases, you may have a child component that does some heavy computation, [...]

Styled Component Properties in React

March 15th, 2024|

By default, properties passed to a styled component are passed on as attributes to the native HTML element created. A regular property must be a valid HTML element attribute, or there will be an error [...]

CSS

Fancy Checkboxes and Toggle Switches

April 5th, 2019|

This allows you to replace default checkboxes with a nicer-looking custom font alternative. This example uses Font Awesome 4 icons, but the CSS could easily be modified to use any font. For the code below [...]

Preserve White Space in Static Text Without Using PRE Tag

March 13th, 2019|

When displaying multi-line text (e.g. input from a TextArea) in a static element (e.g. DIV), extra white space and line breaks are ignored, resulting in one long line of text. An older approach used to [...]