javascript function to add to pulldown menu -
okay, have pulldown menu looks this:
<form action="" method="post" name="myform"> <select name="test_select"> <option value="1">my val 1</option> <option value="3">my val 3</option> <option value="4">my val 4</option> </select> <input type="submit" value="test_button" name="test_button"> </form>
just basic menu. how can search through , add value 2 menu function? want end looking this:
<form action="" method="post" name="myform"> <select name="test_select"> <option value="1">my val 1</option> <option value="2">my val 2</option> <option value="3">my val 3</option> <option value="4">my val 4</option> </select> <input type="submit" value="test_button" name="test_button"> </form>
i tried making function, show trying go this.
<script> function additems() { var option = document.createelement("option"); option.text = "val 2" option.value = 2 (var = 1; < 3; i++; ) { if (document.getelementsbyname.value < 2) { option.text = "val 2" option.value = 2 } } } </script>
how make function work?
i modified code bit make work
function additems() { var option = document.createelement("option"); option.text = "val 2"; option.value = 2; var select = document.getelementsbyname('test_select'); var firstgreater = null; if (select.length > 0) { for(var = 0; < select[0].children.length; i++) { if (select[0].children[i].value > 2) { firstgreater = select[0].children[i]; break; } } if (firstgreater != null) select[0].insertbefore(option, firstgreater); else select[0].appendchild(option); } }
this snippet not need addition library, can see live demo here on jsbin
Comments
Post a Comment