I'm new to Javascript, and I am trying to pass two parameters to a function which then submits them via a form, but I get an ReferenceError when I click the image that acts as a function caller. These are my inputs, a text area and three radio buttons:
<label for="searchString">
<input type="text" id="searchString" name="searchString" maxlength="20"> </br>
</label>
<section id="searchMode" style="margin-top: 3px; margin-bottom: 2px">
<input type="radio" id="Title" name="searchMode" value="Title"/>
<label for="Title">Title</label>
<input type="radio" id="Author" name="searchMode" value="Author"/>
<label for="Author">Author</label>
<input type="radio" id="Number" name="searchMode" value="Number"/>
<label for="Number">Number</label>
</section>
And this is the search function:
<script>
s = document.getElementById("searchString").value;
opt = document.querySelector('input[name="searchMode"]:checked').value;
</script>
<section style="margin-top: 3px;">
<a href="javascript:search(s,opt)">
<img alt="search" id="searchImage" src="images/search.png" width="22" height="22">
</a>
</section>
The error message I am seeing is:
Uncaught ReferenceError: opt is not defined
I've tried declaring the two variables without let
or var
but that doesn't seem to work, how can I fix my code so I don't get this error?
EDIT: I think the issue might be that I can't properly pass my parameters from my form to the controller; the search function gets called and I get no error but they are received as undefined
.
Here are the form and the function:
<form name="searchForm" method="post" action="Dispatcher">
<input type="hidden" name="searchString"/>
<input type="hidden" name="searchMode"/>
<input type="hidden" name="controllerAction" value="ProductsManagement.view"/>
</form>
function search(s,opt){
const f = document.searchForm;
f.searchString.value = s;
f.searchMode.value = opt;
f.submit();
}