jquery - Add new <option> to all dropdown <select> tags using javascript -
im trying add drop down menu option "please select" top of each drop-down list on page selected default, using similar this:
window.onload = function() { $('select option[value="psc"]').attr("selected",true); };
and im using drop-down:
window.onload = function additem(text,value) { // create option object var opt = document.createelement("option"); // add option object drop down/list box document.getelementsbytagname("option").options.add(opt); // assign text , value option object opt.text = 'please select...'; opt.value = 'please select'; }
i'm new @ javascript, can point me in right direction compiling when page loads every drop-down list gets default selected option called "please select"
thanks in advance
modified pure javascript function function (this not affect selected option) demo jsfiddle
window.onload = function additem(text, value) { // drop down/list box in document var sel = document.getelementsbytagname("select"); (var x = 0; x < sel.length; x++) { // create option object , set it's value/text var opt = document.createelement("option"); opt.text = 'please select...'; opt.value = 'please select'; //prepend in select box sel[x].insertbefore(opt, sel[x].options[0]) } }
Comments
Post a Comment