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.

6
  • What is the exact error message? Commented Jul 19, 2023 at 9:14
  • It should rather be value="getSelectValue(this)". I doubt you want to run this function server-side Commented Jul 19, 2023 at 9: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. Commented Jul 19, 2023 at 9:16
  • 1
    Pass a javascript variable value into input type hidden value Commented Jul 19, 2023 at 9:18
  • exact error is Call to undefined function getSelectValue() (View: D:\Softwares\xampp6\htdocs\lms\resources\views\admin\webinars\modals\audio.blade.php) Commented Jul 19, 2023 at 9:30

1 Answer 1

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
}
Sign up to request clarification or add additional context in comments.

2 Comments

I want to get the selected value in hidden input value
document.getElementById("hiddenField").value = lang; andd this instead of console.log()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.