Search code examples
javascriptjqueryformsasync-awaitapi-gateway

adding parameter from html form to jquery string


I am trying to add to the below async call in javascript which sends parameter q=<query>, the second parameter l=<lquery>, but I don't see it sent out, checked in dev tools console

var searchbox = $('input#search');
var langquery = $('input#fav_language');
var timer = 0;

// Executes the search function 250 milliseconds after user stops typing
searchbox.keyup(function () {
  clearTimeout(timer);
  timer = setTimeout(search, 250);
});

async function search() {
  // Clear results before searching
  noresults.hide();
  resultdiv.empty();
  loadingdiv.show();
  // Get the query from the user 
  let query = searchbox.val();
  // Only run a query if the string contains at least three characters
  if (query.length > 2) {
    let lquery = langquery.val();   
    // Make the HTTP request with the query as a parameter and wait for the JSON results
    let response = await $.get(apigatewayendpoint, { q: query, l: lquery, size: 25 }, 'json');

the html body is below. i've tried referring to the radio form id an to the input name, both didn't worked

<body>
  <h1>InfoLang - Movie Plot At Your Language</h1>
        
  <form id="lang">
  <p>Please select language:</p>
  <input type="radio" id="it" name="fav_language" value="Italian">
  <label for="it">Italian</label><br>
  <input type="radio" id="fr" name="fav_language" value="French">
  <label for="fr">French</label><br>
  <input type="radio" id="he" name="fav_language" value="Hebrew">
  <label for="he">Hebrew</label>
  <br>  
  </form>
  
   
  <hr>
  <input id="search" autocomplete="off" placeholder="Search your movie" align="center">

Solution

  • i got this by-

    if (query.length > 2) {
        let lquery
        if (document.getElementById('it').checked) lquery = 'it'
        if (document.getElementById('fr').checked) lquery = 'fr'
    ...
    

    and for the display

    let title = results[item]._source.title
            if (lquery == 'it') plot = results[item]._source.description_it
            if (lquery == 'fr') plot = results[item]._source.description_fr