ted_chou12 Posted November 6, 2007 Share Posted November 6, 2007 Is there an easy javascript that allow dropdown list to stay on a default value? eg. when u press the color dropdown list, it still stays on Change Color, eventough I clicked red or any other colors in the dropdown list. Thank you. Ted Quote Link to comment Share on other sites More sharing options...
obsidian Posted November 6, 2007 Share Posted November 6, 2007 I'm not sure if I'm understanding. If it's a javascript solution you're asking about, this implies that your page is not reloading. If your page is not reloading, your select box will not change from the selected component. If your page is reloading, you really need to modify your form to "remember" the selected value via PHP. Here's a simple example: <?php $val = 0; if (isset($_POST['dropdown'])) { $val = $_POST['dropdown']; echo "You have chosen {$val}"; } ?> <form name="my_form" action="" method="post"> <select name="dropdown" onchange="this.form.submit()"> <?php $val = isset($_POST['dropdown']) ? $_POST['dropdown'] : 0; $options = array(1,2,3,4,5); foreach ($options as $o) { echo "<option value=\"{$o}\""; echo $o == $val ? ' selected="selected"' : ''; echo ">{$o}</option>\n"; } ?> </select> </form> Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted November 7, 2007 Author Share Posted November 7, 2007 sorry, maybe I didn't explain myself well enough, what I mean is without reloading the page, the dropdown list stays on the same value. Eg. when you reply to this thread, u can see a "change color" dropdown, and whatever color u click, it still stays on "change color". Thanks Ted. Quote Link to comment Share on other sites More sharing options...
obsidian Posted November 7, 2007 Share Posted November 7, 2007 sorry, maybe I didn't explain myself well enough, what I mean is without reloading the page, the dropdown list stays on the same value. Eg. when you reply to this thread, u can see a "change color" dropdown, and whatever color u click, it still stays on "change color". Thanks Ted. Well, if the page doesn't reload, by default a select box stays on whatever value you select. Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted November 7, 2007 Author Share Posted November 7, 2007 Sorry for not being clear, I mean eventhough you click on any value, it applies to the textarea, but the dropdown list still stays on "Change Color" without refreshing the page. *example, if you reply or post new threads on phpfreaks, the dropdown for change color in the textarea works this way. Thanks Quote Link to comment Share on other sites More sharing options...
obsidian Posted November 7, 2007 Share Posted November 7, 2007 Sorry for not being clear, I mean eventhough you click on any value, it applies to the textarea, but the dropdown list still stays on "Change Color" without refreshing the page. *example, if you reply or post new threads on phpfreaks, the dropdown for change color in the textarea works this way. Thanks Ah, so you are wanting the dropdown to stay on the initial option after selection? You just need to have a simple javascript function something like this: Javascript: function resetSelect(ele) { ele.selectedIndex = 0; } HTML: <select name="my_select" onchange="resetSelect(this);"> <option value="">Choose One</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> </select> Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted November 7, 2007 Author Share Posted November 7, 2007 thanks, that was much simpler than i thought. , worked perfectly! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.