How to prevent ejs from splitting a string in attribute-value pair in a form field

TL:DR – put quotes around your EJS value when it is being used as an HTML attribute

I was making a simple Express app with EJS templating and MongoDB for my REST API. I passed the response, which was a few strings and a date, to the view in the typical fashion, and I could verify that the data was being passed by outputting the object to the view like this:

<%=responseObject%>
//which displayed this:
{ 
  _id: 5d347b004b0c201108909a55, 
  name: 'hot dogs', 
  description: 'costco dogs', 
  dateOnBag: '3/4/19', __v: 0 
}

This displayed the entire object on the screen. Additionally, I could display one of the object’s values using dot notation with no problem.

<h1>Edit <%=responseObject.name%></h1>
// which displayed this

Edit hot dogs

However, when I tried to use the same syntax to prefill a form field like this:

<li>
  <input type="text" name="name" value=<%-responeseObject.name%>><br />
  <label for="name">Item Name </label>
<li>

It pre-filled only the first word, “hot” instead of both words “hot dog”


I finally figured out that I needed to put quotes around it. When Express renders the page it simply replaces the EJS syntax with a string of text so the resulting HTML looked more like this:

  <input type="text" name="name" value=hot dog><br />

I later learned that HTML attribute values do not always need quotes. (basically, when there are no spaces) https://mathiasbynens.be/notes/unquoted-attribute-values So in the example above,

value=hot dog
//is equivalent to 
value="hot" dog

which leaves dog just hanging out there, an attribute without any value which HTML just ignores in this case. Actually, if I’m interpreting the spec correctly, it is setting the value of dog to true which is any case is not used so again it is ignored. https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes

Web Scraping with Cheerio.js

When learning web development, they all tell you to find a problem in your life and create a solution using code to create a great project. Well, I drive for Uber and I wanted to know when the most flights would be coming into my local airport. The airport website has listings, but it’s a cluttered mess. I wanted something where I could just see at a glance when the most flights were coming in. I could not find a public/free API for flight information so I needed to figure out a little bit of web scraping. Code and working example is here: https://codesandbox.io/s/7zx3v5kqmq and I did a video walk-through explaining how the code works here:

Algorithms!

I’ve been making more videos and this time I’ve been working on the freeCodeCamp selection of algorithm challenges. I’ve been doing one every day and adding them to the playlist below. I’m twelve days in and the plan is to do one every day until I’ve got them all!

Just Enought JavaScript To Be Dangerous

I made a series of videos on my youtube channel about the basics of JavaScript. These might not be the best for the total beginner and it’s not meant to be a “learn everything about JavaScript deeply” kind of a course. They might be best suited for someone who has started to learn JavaScript and needs a refresher. It goes pretty quickly and it might gloss over a few things. My goal was to get to doing something more useful quickly. I’m pretty sure nobody ever said “The day I knew web development was for me was the day the computer confirmed for me that racecar spelled backward is in fact racecar”. I wanted to get to making things actually happen on the page ie. DOM manipulation, event listeners, etc. sooner than later.

CSS animation basics and Sass mixins

This was based on something a person was asking about on a facebook group. They were asking if there was a library that would make background animations. I saw their example and said “That looks like the bottom of 3 moving circles. I bet I can make that with just CSS”. Here is the result of what I came up with. I ended up reviewing about SCSS mixins to reduce the amount of code. Below you’ll find my codepen as well as a video where I walk through my process.

Responsive landing page based on client requirements.

Huddle landing page

This was a project from www.frontendmentor.io The site provides practice challenges that users can work through.  They provide a project brief, style guide, and an image of the design and you have to recreate it yourself.  If you’re trying to get out of the tutorial rut but need some inspiration on what to build yourself, this is a great resource.

Event Delegation in JavaScript

You don’t always have to write event listeners for everything. Event delegation lets you add one listener to the parent element and then use the information from the created event object to tell which child element was the target of the event. Here’s how it’s done.