css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation


Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

              <form id="example_form">
  <div class="address_container">
    <div class="form-group">
      <label for="address">Search for address or postcode</label>
      <input type="text" class="form-control" id="address" placeholder="e.g. Manor Farm Barns, Framingham Pigot or NR14 7PZ" aria-owns="address_result" autocomplete="off" autocorrect="off">
    <button type="submit" class="btn btn-default" id="search">Search</button>
    <div id="address_status" aria-live="assertive" aria-atomic="true" role="status" class="sr-only"></div>
    <div id="address_results"></div>


<form id="output_form">
  <div class="form-group">
    <label for="address_line_1">Address line 1</label>
    <input type="text" class="form-control" id="address_line_1">
  <div class="form-group">
    <label for="address_line_2">Address line 2</label>
    <input type="text" class="form-control" id="address_line_2">
  <div class="form-group">
    <label for="posttown">Post town</label>
    <input type="text" class="form-control" id="posttown">
  <div class="form-group">
    <label for="postcode">Postcode</label>
    <input type="text" class="form-control" id="postcode">
<small>Test API key always returns data for NR14 7PZ</small>
              body {
  padding: 5%;
#address {
  margin-bottom: 20px;
#address_results {
  padding-top: 20px;
#address_results .radio {
  padding: 5px;
#address_results .radio:hover {
  background: #eee;

              // Replace with your API key, test key is locked to NR14 7PZ postcode search
var api_key = "PCW45-12345-12345-1234X";

var example_form = document.getElementById("example_form");

example_form.addEventListener("submit", function(e) {

  // The field we will use for search term input
  var address = document.getElementById("address");

  // Where we will put our search results
  var address_results = document.getElementById("address_results");

  // Accessible status messages for assisted technologies like screen readers
  var address_status = document.getElementById("address_status");

  address_search(address, address_results, address_status);

function address_search(input_element, address_results, status_element, page) {
  var page = page || 0;

  var address = input_element.value.trim();

  if (address != "") {
    // Remove any previous validation results
    if (page == 0) address_results.innerHTML = "";
    // Create a loading message
    var loading_html = document.createElement("div");
    loading_html.setAttribute("id", "address_loading");
    loading_html.textContent = "Searching addresses...";

    input_element.insertAdjacentElement("afterend", loading_html);

    status_element.textContent = "Searching addresses";

    // Country hard coded to GB for this example
    var country_code = "GB";
    // Create the URL to API including API key and encoded search term
    var address_url =
      "https://ws.postcoder.com/pcw/" +
      api_key +
      "/address/" +
      country_code +
      "/" +
      encodeURIComponent(address) +
      "?lines=2&page=" +
    // Call the API
    var address_request = new XMLHttpRequest();
    address_request.open("GET", address_url, true);

    address_request.onload = function() {
      if (address_request.status >= 200 && address_request.status < 400) {
        var data = JSON.parse(address_request.responseText);

        // For only one result, simply populate the fields, rather than asking the user to select from list
        if (data.length == 1) {
          select_address(data[0], address_results, status_element);
          status_element.textContent =
            '"' +
            data[0].summaryline +
            '" selected, address fields below populated';
        } else if (data.length > 1) {
          for (var i = 0; i < data.length; i++) {
            var address_option = document.createElement("div");
            address_option.className = "radio";

            address_option.onclick = (function(item) {
              return function(e) {
                // Only trigger if it's a click with the mouse, not caused by using arrow keys
                if (e.type === "click" && e.offsetX !== 0 && e.offsetY !== 0) {
                  select_address(item, address_results, status_element);

            address_option.onkeydown = (function(item) {
              return function(e) {
                // When using the keyboard, on return/enter select the item
                var code = e.keyCode || e.which;
                if (code == 13) {
                  select_address(item, address_results, status_element);

            var address_option_label = document.createElement("label");
            address_option_label.textContent = data[i].summaryline;

            var address_option_radio = document.createElement("input");
            address_option_radio.setAttribute("type", "radio");
            address_option_radio.setAttribute("name", "address_radio");
              "address_" + page + "_" + i
            address_option_radio.value = page + "_" + i;

            // Insert radio into label, at start

            // Insert label into option div, at end

            // Insert option div into output_element, at end
            address_results.insertAdjacentElement("beforeend", address_option);

          // Check if we have more than one page of results (Slight edge case)
          // Either let your user page through the results using button or
          // show a message to encourage them to refine their search.
          // Typically adding a house number or name along with postcode helps

          var last_index = data.length - 1;

          try {
            // Remove existing more button, if exists
          } catch (e) {}

          if (data[last_index].morevalues) {
            // Create the more button and add some context to text, using totalresutls element
            var show_more_button = document.createElement("button");
            show_more_button.textContent =
              data[last_index].totalresults +
              " addresses found, click to show next 100";
            show_more_button.setAttribute("id", "show_more_button");

            // Bind a click to the button which will pass the nextpage element through
            show_more_button.onclick = (function(last_item) {
              return function(e) {

            // Insert button at end of existing results

            // Accessible status message to say more than 100 addresses
            status_element.textContent =
              data[last_index].totalresults +
              " addresses found, showing 100 per page. This is page " +
              (parseInt(page) + 1) +
              ". Use tab to reach next page button, after address results.";
          } else {
            status_element.textContent = data.length + " addresses found";
        } else {
          address_results.textContent = "No addresses found";
          status_element.textContent = "No addresses found";
      } else {
        status_element.textContent = "Error occurred";
        address_results.textContent = "Error occurred";
        // Triggered if API does not return HTTP code between 200 and 399
        // More info - https://developers.alliescomputing.com/postcoder-web-api/error-handling
    address_request.onerror = function() {

      status_element.textContent = "Error occurred";

      address_results.textContent = "Error occurred";
      // Triggered if API cannot be reached
      // More info - https://developers.alliescomputing.com/postcoder-web-api/error-handling
  } else {
    // Could show an "Address search term is required" message here

function select_address(address, address_results, status_element) {
  address_results.innerHTML = "";

  status_element.textContent =
    '"' + address.summaryline + '" selected, address fields below populated';

  // Populate fields
  document.getElementById("address_line_1").value = address.addressline1 || "";
  document.getElementById("address_line_2").value = address.addressline2 || "";
  document.getElementById("posttown").value = address.posttown || "";
  document.getElementById("postcode").value = address.postcode || "";

  // Full list of response elements
  // https://developers.alliescomputing.com/postcoder-web-api/address-lookup

🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................