Passing a JavaScript variable to fill innerHTML


Ask A Question





Login To post a question

I’m trying to pass a simple variable from a form input to populate a span with the .innerHTML syntax. My code (with search.ejs) is as follows:

<script>function readSearchValue() {
      var passedSearchValue = document.getElementById('searchForm').submit();
      document.getElementById('mySearch').innerHTML = passedSearchValue;
    }
  </script>

  <!-- Search form -->
  <nav class="navbar navbar-light bg-light">
    <form class="form-inline" id="searchForm">
      <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search" id="searchValue">
      <button class="btn btn-outline-success my-2 my-sm-0 glyphicon glyphicon-search" type="submit" onclick="readSearchValue()"></button>
    </form>
  </nav>

<p>You have searched for:
    <span id="mySearch"></span>
  </p>

I also have a search.js file which doesn’t do much ATM:

res.render('search', {
        title: 'Search results',

I get an undefined error with the code above. Thanks in advance for any help!

Kelly.

For more info please click herehttps://stackoverflow.com/q/51811275



Have a question or need a custom quote?
info@smartinfocare.com