Vinay Vinay - 1 month ago 6
HTML Question

Can i use <select> without including it inside a <form>

I just need to select a value from a dropdown list but i don't need to post it to other page. I just need the value to process on the same page. Is there a way to do that without making a form? Because the for will provide the select value only when i press the submit button. But i don't need a button just a single dropdown menu.
Would you please also tell me how to store the value of the into a php variable?

Answer

yes you can use it without <form> tag.

in fact you can use any form related element without the <form> tag.

if you want to use the <select> elemet selected value you can use select.onchange event.

the element value is selected option value.

html Code:

<select id='select'>
    <option value='11'>
        testOption
    </option>
</select>

js Code:

var select = document.getElementById('select');
select.onchange = function () {
    var value = this.value;
    alert(value);
}