-1

Below is my HTML and I want to call a JS function every time my input field's value changes inside "foundation-autocomplete" element

<foundation-autocomplete class="coral-Form-field" pickersrc="/mnt/overlay/granite/ui/content/coral/foundation/form/pathfield/picker.html?_charset_=utf-8&amp;path={value}&amp;root=%2fcontent&amp;filter=hierarchyNotFile&amp;selectionCount=single"> <div class="foundation-autocomplete-inputgroupwrapper"> <div class="coral-InputGroup" role="presentation"> <input is="coral-textfield" class="coral3-Textfield coral-InputGroup-input" autocomplete="off" placeholder="" id="coral-id-106" role="combobox" aria-expanded="false" aria-invalid="false" value="/swing"> <span class="coral-InputGroup-button"> <button is="coral-button" class="coral3-Button coral3-Button--secondary" size="M" variant="secondary" title="Open Selection Dialog" type="button" aria-label="Open Selection Dialog"> <coral-icon class="coral3-Icon coral3-Icon--sizeS coral3-Icon--select" icon="select" size="S" autoarialabel="on" role="img" aria-label="select"></coral-icon> <coral-button-label></coral-button-label> </button> </span> </div> </div> </foundation-autocomplete> 

I tried with above code but function call is not happening. Is there any other way ? ` // Select the element const foundationAutocomplete = document.querySelector('foundation-autocomplete');

// Create a MutationObserver instance const observer = new MutationObserver(function(mutationsList, observer) { // Call your JavaScript function here when a mutation occurs yourFunction(); }); // Define your JavaScript function function yourFunction() { // Your code here console.log('foundation-autocomplete value changed.'); } // Configure the MutationObserver to watch for changes to the subtree and attributes observer.observe(foundationAutocomplete, { subtree: true, attributes: true }); 

`

3
  • 3
    I think you want an event listener, rather than a mutation observer. Commented Sep 7, 2023 at 17:14
  • You can just have a change event for the input element inside the container. .coral-Form-field input Commented Sep 7, 2023 at 17:18
  • $("div.foundation-autocomplete-inputgroupwrapper").on("input", function(){....}); Commented Sep 7, 2023 at 17:29

1 Answer 1

0

This will run yourFunction when the input changes:

$(document).ready(function(){ $('#coral-id-106').on('change', () => yourFunction() ) }) 

Here's what that means:

  • $(document).ready(function(){ When the web page finishes loading.
  • $('#coral-id-106').on( Add an event listener to the element with an id of 'coral-id-106.'
  • 'change' The type of event listener - in this case we want something to happen when the element changes.
  • () => shorthand for function (){
  • yourFunction() - The function you want to get triggered when the element changes.
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.