javascript - How do I clear a form and reset a drop down menu to the first option? -
i have form has text fields, drop down menu, radio buttons, , check boxes. able clear of input entered user need modify form when user clicks "clear entries" of above fields cleared , drop down menu return default state of "pa".
function doclear() { document.pizzaform.customer.value = ""; document.pizzaform.address.value = ""; document.pizzaform.city.value = ""; document.pizzaform.state.value = ""; document.pizzaform.zip.value = ""; document.pizzaform.phone.value = ""; document.pizzaform.email.value = ""; document.pizzaform.sizes[0].checked = false; document.pizzaform.sizes[1].checked = false; document.pizzaform.sizes[2].checked = false; document.pizzaform.sizes[3].checked = false; document.pizzaform.toppings[0].checked = false; document.pizzaform.toppings[1].checked = false; document.pizzaform.toppings[2].checked = false; document.pizzaform.toppings[3].checked = false; document.pizzaform.toppings[4].checked = false; document.pizzaform.toppings[5].checked = false; document.pizzaform.toppings[6].checked = false; document.pizzaform.toppings[7].checked = false; document.pizzaform.toppings[8].checked = false; return; }
this html form looks like:
<form> <font size="3">state:</font> <select name="state"> <option selected>pa</option> <option value="nj">nj</option> <option value="ny">ny</option> <option value="de">de</option> </select> </form>
i have pa selected suggestion however, not select pa when clear entries button clicked.
update html pa option has value
<option selected value="pa">pa</option>
add following js:
var elements = document.getelementsbytagname('select'); elements[0].value = 'pa';
a better way select set id on it
html
<select name="state" id="myselect">
js
document.getelementbyid('myselect'); element.value = 'pa';
this prevent issues have if added select, (unless added select same id invalid html anyways)
jsfiddle demo
Comments
Post a Comment