Javascript help

Hi all,

Im hoping someone can help me with this bit of javascript. I have two search views onb one page and want the user to only search in 1 and have the other search update. So far i have this:

$(document).on('knack-records-render.view_203', function(event, view, records) {
  
    var e = document.getElementById("kn-conn-1-field_142");
    var siteSearch = e.options[e.selectedIndex].text;
    var f = document.getElementById("value");

  //alert('you have searched for ' + siteSearch);

    $('#view_227-field_2').text(siteSearch);
    $('#view_227-field_2').view;

  console.log(siteSearch);


});

However i cant seem to get the second view to receive the sitesearch info. Any help is much appreciated.

the get element by id also appears to be broken, in that it works the first time i load the page, but if i navigate to a different page and back, it fails. I assume this is because it will get a different ID, but im not sure how to get the searched item.

Okay so i think i have cracked this sort of…

i use this code to hide the second search i dont want visible:

//hides the search
$(document).on('knack-scene-render.scene_34', function(event, scene) {

  var $elementToHide = $("#view_227 > form > div.kn-submit.control.is-horizontal");
  $elementToHide.css('visibility', 'hidden');
  $("#view_227-search").css('visibility', 'hidden');

});

then i use this code to grab the searched item and send it to the second search box:

$(document).on('knack-records-render.view_203 , knack-records-render.view_200 , knack-records-render.view_201', async function(event, view, records) {

  Knack.showSpinner();

  // Find the dropdown element by its attributes or position in the DOM.
  var $dropdown = $('[data-field-key="field_142"]');

  if ($dropdown.length > 0) {
  // Get the selected option text from the dropdown.
  var siteSearch = $dropdown.find('option:selected').text();

  console.log(siteSearch);

  $("input[name='value']").val(siteSearch);
  $("input[name='value']").submit();

  Knack.hideSpinner();
});

This seem to be working okay. Its probably not the best way to do it, but it works for me. Hopefully it will help someone else… maybe

1 Like