javascript - Dropdown menu item reversal -
i new javascript , code looks below
in html:
<select name="dropdown" onchange="javascript: alertuser()" > <option value="0" selected>eat</option> <option value="1">work</option> <option value="2">sleep</option> <option value="3" >enjoy</option>
in javascript:
function alertuser() { confirm("are sure?"); }
now, here problem.
when select element dropdown, pop confirmation dialogue "are sure?". if click ok, selection should change selected value. if cancel, selected value should reset selected value in dropdown list.
could please me solve problem.
thank you
you can use this:
window.onload = function () { var select = document.getelementsbyname('dropdown')[0]; var lastselected = select.value; select.onchange = function () { var newselected = select.value; if (confirm("are sure?")) { lastselected = newselected return true; } select.value = lastselected; } };
i added onload run when page loads. removed inline js, more clean. html be:
<select name="dropdown"> <option value="0" selected>eat</option> <option value="1">work</option> <option value="2">sleep</option> <option value="3">enjoy</option> </select>
Comments
Post a Comment