skip to Main Content

I have a select option tags in my HTML which are populated via api calls

<select name="channel" id="selectName" onChange="getSelectedName(this)">
     <option value="">Select Name</option>
</select>

In my JS, I’m getting the values of each option with the following lines of codes

// get selected name
function getSelectedName(selectoption){
    let name = selectoption.value;
    console.log(name);
  }

This log the exact value of the channel in the console which is great.

My Challenge:
I have other functions in my project that need the exact values of the value being generated by that function.

How do I access that exact value globally so as to ingest them into other functions.

What I have done:
I’ve tried making the name global, however I have not had any headway with it.

Thanks in anticipation of your response.

2

Answers


  1. If they are on the same page, you should be able to use

    getElementById("selectName").value
    

    This gets the actual selected value of your <select>

    Login or Signup to reply.
  2. You can access the select element value using getElementById. If you want to run other functions when select event happens use this kind of solution

    function getSelectedName(selectoption){
        let name = selectoption.value;
        function1(name);
        function2(name);
    }
    

    Or you can use event handler

    document.getElementById("selectName").addEventListener("change", function1);
    document.getElementById("selectName").addEventListener("change", function2);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search