Pass the value of a SELECT to a Javascript function via the onchange event?

40,402
<select name="report_type" onchange="hide(this.value);">
<option value="full_history">Full History</option>
<option value="partial_history">Partial History</option>            
</select>

When doing this the function have whatever value the select currently has.

Share:
40,402
John M
Author by

John M

Updated on February 06, 2020

Comments

  • John M
    John M over 4 years

    I have a HTML page that contains a search box containing a number of textboxes.

    The first part of the search box is a SELECT dropdown list that contains a variety of report types. Each report type requires 1 or more of textboxes to be filled in to filter the query results. My goal is hide the textboxes that are not required by the current report type.

    How do I pass the currently selected value from the SELECT to the Javascript function via the onchange event?

    <select name="report_type" onchange="hide();">
    <option value="full_history">Full History</option>
    <option value="partial_history">Partial History</option>            
    </select>
    
  • ZeekLTK
    ZeekLTK over 3 years
    I know this is old but maybe someone will see it - I'm trying to do something like this, but I want to pass the value that is defined in the option tag, rather than the "value" itself. How would I do that? --- For example, in the above code, I want to pass "full_history" (as in value="full_history"), but this.value is passing "Full History".
  • gabe3886
    gabe3886 over 3 years
    @ZeekLTK it sounds like the options don't have the value attribute set, and are therefore taking the text from the option itself. If you put your code in another question then it may provide more insight than I can gain from simply a comment.
  • ZeekLTK
    ZeekLTK over 3 years
    Thanks Gabe, it was too late at night I guess, I accidentally left out or deleted the equal sign, so it was just value"full_history" (using the above example). Once I fixed the syntax (to value="full_history") it is passing what I wanted. Thanks.