-1

Most of the solutions I have found deal with localized javascript instead of an external file so I am posting this question. Links to attempted solutions:

Turn HTML Form Input into JavaScript Variable

I want to take a html form input and use that input in an external javascript file.

The html form input comes from index.html

<script src="script/our.js"></script>
<div class="searchbar">
    <form action="">
        <input type="text" placeholder="search ..." name="query" onchange=formChange()>
        <button type="submit">sendit bro</button>
    </form>
</div>
<script>
    function formChange(){
        var searchThis = document.getElementsByName("query").value;
    }
</script>

Then, our.js tries to use the variable like so:

var Manager;
(function ($) {
    $(function () {
        Manager.store.addByValue('q', searchThis);
    });
})(jQuery);

With my current setup, if I searched a term ("unlock") the form will send and the page will show it has found 0 documents. It does not say that searchThis is undefined The back end of my js works with searchThis being replaced by "*:*" to display all records instead of those with the searched string.

EDIT #1 : Manager.jquery.js to see if I need to allow for search term to be a variable an not just a string REDACTED

EDIT #2 : I have been trying suggestions below. Currently:

index.htmlchanged

<script>
    function formChange(){
        var searchThis = document.getElementsByName("query")[0].value;
        console.log(searchThis);
        window.searchThis = searchThis;
        console.log(window.searchThis);
    }
</script>

our.jschanged

    Manager.store.addByValue('q', window.searchThis);

The console.logs in index.html return my search term. However if I do the same just before window.searchThis is called in our.js it returns as undefined.

EDIT #3 I have been playing with importing the searchThis variable. I cannot import window.searchThis because I get an error for unexpected . In full when I try:

import searchThis from "../index.html"

It gives me an error stating:

Failed to load module script: The server responded with a non-JavaScript MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.

EDIT #4 : my <form> + <script> section with adjustments

<div class="searchbar">
    <form>
        <input type="text" placeholder="search Nicola..." name="query" onsubmit=formChange() >
        <button type="submit">sendit bro</button>
    </form>
</div>
<script>
    function formChange(){
        searchThis = document.getElementsByName("query")[0].value;
        term = searchThis;
        console.log(term);
    }
</script>
<script defer="defer" src="script/our.js" type="module"></script>

Ive added my form section because I could be doing something wrong here too. When I go to my main page the console.log on our.js has already been logged as undefined. When I enter a search term with onchange instead of onsubmit the console logs from my <script>. Once I hit submit, the page flashes and the log for our.js is again at the top and undefined.

EDIT #5 : pic of logs described above (before submit) enter image description here

Newb 4 You BB
  • 1,205
  • 1
  • 11
  • 30

1 Answers1

0

You could use the window object.

Add a new property named searchThis to the window object and assign the obtained value of the searchThis variable.

function formChange() {
  var searchThis = document.getElementsByName("query")[0].value;
  window.searchThis = searchThis;
}

Then, just call it by using window.searchThis:

var Manager;
(function($) {
  $(function() {
    Manager.store.addByValue('q', window.searchThis);
  });
})(jQuery);

Update: document.getElementsByName("query") returns a NodeList. You could use document.getElementsByName("query")[0] to refer to a single element.

The availability of a variable for reuse in another external file depends on the order of loading or initialization.

The issue that you have is that our.js is running before the window.searchThis is declared.

You can do:

  1. Add a defer attribute in the script tag. Example: <script defer="defer" src="script/our.js"></script>.

OR

  1. Put the <script src="script/our.js"></script> at the end.

Something like this:

<script>
  function formChange() {
    var searchThis = document.getElementsByName("query")[0].value;
    window.searchThis = searchThis;
  }
</script>
<script src="script/our.js"></script>
  • Unfortunately this didn't work for me. If the `window.searchThis` in `our.js` is replaced with a string like "unlock" it will display all documents related to that term. If that function is expecting a string instead of a variable, would that cause issues? I haven't gotten an error when using a variable in place of the search term. – Newb 4 You BB Apr 01 '19 at 16:16
  • Ive added `console.log(document.getElementsByName("query").value)` right after `var searchThis...` The console says it is undefined. – Newb 4 You BB Apr 01 '19 at 19:27
  • 1
    `document.getElementsByName("query")` returns a `NodeList`. You should use `console.log(document.getElementsByName("query")[0].value)`. I've updated my answer. – Danny Fardy Jhonston Bermúdez Apr 01 '19 at 19:32
  • Awesome, so this did allow `window.searchThis` in `our.js` to be my search term. Now when I access the same `window.searchThis` in `Manager.jquery.js` it is shown as undefined. – Newb 4 You BB Apr 01 '19 at 19:43
  • `window.searchThis` is declared like any type. It can be a `string`, `number`, `boolean`, `object`, `array`. It depends its contents. – Danny Fardy Jhonston Bermúdez Apr 01 '19 at 19:43
  • The availability of a variable for reuse in another external file depends on the order of loading or initialization. You could add a `defer="defer"` attribute in the: ``. – Danny Fardy Jhonston Bermúdez Apr 01 '19 at 19:45
  • An edit to my 3rd comment here: `our.js` **->** `index.html` & `Manager.jquery.js` **->** `our.js` – Newb 4 You BB Apr 01 '19 at 19:50
  • unfortunately Im still not able to get access to `window.searchThis` in `our.js`. This is after ive made your suggested changes. The `console.log(window.searchThis)` in `index.html` is correct but when logged just before `Manager.store.addByValue` it shows as undefined. I have tried the defer method and moving the ` – Newb 4 You BB Apr 01 '19 at 20:55
  • I have been playing with `defer` as well as moving the ` – Newb 4 You BB Apr 02 '19 at 15:37
  • The page flashes because you are submitting the form with default form submission action, so your will never run that you want to do. Can you add an `id` attribute in the `
    ` tag?
    – Danny Fardy Jhonston Bermúdez Apr 02 '19 at 17:00
  • I can, I just added `id="searchform"` to the `
    ` tag
    – Newb 4 You BB Apr 02 '19 at 17:05
  • I see... You must run `Manager.store.addByValue('q', searchThis);` only when you submit the form. What is Manager.jquery.js? – Danny Fardy Jhonston Bermúdez Apr 02 '19 at 17:17
  • Other observation is in our.js file. `Manager.store.addByValue('q', window.searchThis);` The `Manager` object always is `undefined`, so you can't call `Manager.store.addByValue`. – Danny Fardy Jhonston Bermúdez Apr 02 '19 at 17:49
  • for some context Im trying to follow sections of the reuters ajax solr tutorial https://github.com/evolvingweb/ajax-solr/wiki/Reuters-tutorial in this they are able to use that manager but leave "*:*" as the string. – Newb 4 You BB Apr 02 '19 at 17:55
  • 1
    Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/191123/discussion-between-danny-fardy-jhonston-bermudez-and-newb-4-you-bb). – Danny Fardy Jhonston Bermúdez Apr 02 '19 at 19:45
  • 1
    @Newb4YouBB I've made a simple demo in Plunker recreating your scenario in this link: https://next.plnkr.co/edit/OcHSHFuJrtdLl6ft?open=lib%2Fscript.js&preview. – Danny Fardy Jhonston Bermúdez Apr 02 '19 at 20:39
  • 1
    I haven't had a chance to use your demo yet, but I wanted to thank you ahead of time. Always appreciate those who are willing to assist beyond expectation. Thank you. – Newb 4 You BB Apr 03 '19 at 13:52