0

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.

Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
  • What is the exact error message? – Konrad Jul 19 '23 at 09:14
  • It should rather be `value="getSelectValue(this)"`. I doubt you want to run this function server-side – Konrad Jul 19 '23 at 09:15
  • 1
    @Konrad `value="getSelectValue(this)"` won't execute any client-side JS either though; all that does is set the _literal_ text `getSelectValue(this)` as value for that field. – CBroe Jul 19 '23 at 09:16
  • 1
    [Pass a javascript variable value into input type hidden value](https://stackoverflow.com/questions/7764154/pass-a-javascript-variable-value-into-input-type-hidden-value) – CBroe Jul 19 '23 at 09:18
  • exact error is Call to undefined function getSelectValue() (View: D:\Softwares\xampp6\htdocs\lms\resources\views\admin\webinars\modals\audio.blade.php) – Maryam Aftab Jul 19 '23 at 09:30
  • I want to get the value of select in hidden input value – Maryam Aftab Jul 19 '23 at 09:32

1 Answers1

1

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
}
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85