How to change input value on amp-autocomplete select

225 Views Asked by At

Trying to create a live search with redirecting to url on amp-autocomplete select.

    <!DOCTYPE html>
    <html ⚡>
      <head>
        <meta charset="utf-8" />
        <link rel="canonical" href="self.html" />
        <meta name="viewport" content="width=device-width,minimum-scale=1" />
        <style amp-boilerplate>
          body {
            -webkit-animation: -amp-start 8s steps(1, end) 0s 1 normal both;
            -moz-animation: -amp-start 8s steps(1, end) 0s 1 normal both;
            -ms-animation: -amp-start 8s steps(1, end) 0s 1 normal both;
            animation: -amp-start 8s steps(1, end) 0s 1 normal both;
          }
          @-webkit-keyframes -amp-start {
            from {
              visibility: hidden;
            }
            to {
              visibility: visible;
            }
          }
          @-moz-keyframes -amp-start {
            from {
              visibility: hidden;
            }
            to {
              visibility: visible;
            }
          }
          @-ms-keyframes -amp-start {
            from {
              visibility: hidden;
            }
            to {
              visibility: visible;
            }
          }
          @-o-keyframes -amp-start {
            from {
              visibility: hidden;
            }
            to {
              visibility: visible;
            }
          }
          @keyframes -amp-start {
            from {
              visibility: hidden;
            }
            to {
              visibility: visible;
            }
          }
        </style>
        <noscript
          ><style amp-boilerplate>
            body {
              -webkit-animation: none;
              -moz-animation: none;
              -ms-animation: none;
              animation: none;
            }
          </style></noscript
        >
        <script async src="https://cdn.ampproject.org/v0.js"></script>
        <script async custom-element="amp-form" src="https://cdn.ampproject.org/v0/amp-form-0.1.js"></script>
        <script async custom-element="amp-autocomplete" src="https://cdn.ampproject.org/v0/amp-autocomplete-0.1.js"></script>
        <script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.2.js"></script>
      </head>
      <body>
        <form class="sample-form" method="post" target="_top" action-xhr="https://amp.dev/documentation/examples/api/echo">
          <label>
            <span>Search for</span>
            <amp-autocomplete filter="token-prefix" filter-value="h1" min-characters="2" on="select:AMP.navigateTo(url=event.value, target=_blank)">
              <input type="search" name="h1" />
              <script type="application/json">
                {
                  "items": [
                    {
                      "h1": "page1",
                      "url": "https://stackoverflow.com/"
                    },
                    {
                      "h1": "page2",
                      "url": "https://google.com/"
                    },
                    {
                      "h1": "page3",
                      "url": "https://amp.dev/"
                    }
                  ]
                }
              </script>
              <template type="amp-mustache" id="amp-template-custom">
                <div class="slug-item" data-value="{{url}}">
                  <span class="autocomplete-link">{{h1}}</span>
                </div>
              </template>
            </amp-autocomplete>
          </label>
        </form>
      </body>
    </html>

In the snippet above on option select, page is successfully redirected to {{url}} and the problem is that input value is changed to {{url}} as well:

enter image description here

How I can use {{h1}} value in input on option select, but leave data-value="{{url}}" in mustache, so on="select:AMP.navigateTo(url=event.value)" will still work?

Tried to combine values in amp-mustache and split it after, but stuck:

<template type="amp-mustache" id="amp-template-custom">
   <div class="slug-item" data-value="{{h1}},{{url}}">
      <span class="autocomplete-link">{{h1}}</span>
   </div>
</template>

on="select:AMP.navigateTo(url=event.value.split(',')[1], target=_blank)"
0

There are 0 best solutions below