Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Define function in script and call in input value comes undefined function

I called a function within script tag from an input value, but it throws Error — call to undefined function
Here is the function definition

<script>
 function getSelectValue(sel){
    var lang=sel.options[sel.selectedIndex];
   

    return lang;
}
</script>

I called the function here

  <div class="form-group">
              <input type="hidden" id="value" value="{{getSelectValue(this)}}" >

                    <label for="audiolang" class="input-label">Audio Language</label>
                        <select id="audio_lang" name="audio_lang" class="form-control " onchange="getSelectValue(this)">
                        <option selected disabled value="">Select Language</option>
                            <option value="1">English</option>
                            <option value="2">Arabic</option> 
                            <option value="3">Urdu</option>
                            <option value="4">Hindi</option>
                        </select>
                    </div>

Why this error **call to undefined function getSelectValue() ** is coming.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

As I understood you have select in your code and you want to get the selected value.

In select

<select id="mySelect" onchange="setHiddenFieldValue(this)">

In input

<input type="hidden" id="hiddenField">

In function

function setHiddenFieldValue(sel) {
    var lang = sel.options[sel.selectedIndex].value;
    console.log(lang);
    document.getElementById("hiddenField").value = lang; // to set the value
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading