0

I need different default and selected value for HTML <select> tag. It means that I need my <form> load with a selected <option>, then after pressing <button type="reset"> button another option set to select tag, but how?

<select size="1" name="select1" >
    <option  value='1'> default value </option>
    <option selected="true" value="2">selected value</option>
</select>

onclick event for button doesn't works:

<button onclick="resetSearchPanel();" type="reset"> ...

java script function:

 function resetSearchPanel() {

  document.forms['submitSearch'].elements['select1'].value = "1";

}

above snippet works when the button type property setted as 'button' but it doesn't work when type set to reset!

Nik Kashi
  • 4,447
  • 3
  • 40
  • 63

4 Answers4

0

Onclick of reset, just make the required value in the select tag as selected.

on Onclick() method of reset button, make sure that you write a function which does whatever you want to.

If you set the button type as reset, it will reset the whole of form data, so youe select tag will not be having the value you set it to. Remove reset tag, and it should be fine.

If its must for you to use type as "reset" then you have to use work arounds to make sure that your code is executed after the form is reset, but that approach is strongly discouraged.

LPD
  • 2,833
  • 2
  • 29
  • 48
0

The reset button will reset all values back to default. Its onclick happens before the actual reset taking place, so any change you make there will be lost.

Since there is no "onafterclick" even, only way around if you must use reset button is using a timer:

function resetSearchPanel() {
    window.setTimeout(function() {
        document.forms['submitSearch'].elements['select1'].value = "1";
    }, 100);
}

This will change the selected value 0.1 seconds after clicking the button.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
0

You have given button type='reset' dont know why because type reset will clear the form data. So for your problem you need to give the button type='button'

<button onclick="resetSearchPanel();" type="button">

Hope this will work for you.

Ranjit Singh
  • 3,715
  • 1
  • 21
  • 35
  • I need reset button. I have several input in my form and I need reset all to default value after clicking reset. – Nik Kashi Dec 30 '12 at 11:23
-1

Select option you need on page load with JavaScript.

Hott Dogg
  • 11
  • 1