jquery - How to jump to a particular page's some drop-down's particular value using JavaScript? -
suppose have drop-down "page2.html":
page2.html
<div id="mydropdownlink"> <select id="first-drop" name="first-drop"> <option value="select state">select steate</option> <option value="ny">new york</option> <option value="nj">new jersey</option> </select> </div>
now, "page1.html", have link like
page1.html
<a href="abc.html" id="mylink">go page 1</a>
so goal is:
on click of link, go "page2.html" , select second drop down value , selected. example, select second drop down value ie "nj" one. can programmatically?
i able go specific page drop-down location. failed select second value form drop-down.
window.location.href = forwardedwebsitelink + "?" + "#first-drop";
any idea can this?
you combine 2 things: uri anchors (adding #
in uri) , parameters (?
followed values) make example.com#first-drop?firstdropdown=nj
in javascript (on page2.html) you'd have this:
//run script on page load. window.onload = function() { var url = window.location.href; //let me cheat since don't have page1 , page2 in example... url += "#firstdropdown?firstdropdown=ny"; // select text after # , before ? symbol. var dropdown = url.substring(url.indexof('#') + 1, url.indexof('?') - 1); // select text after ? symbol var selectedoption = url.substring(url.indexof('?') + 1); // filter dropdown's id , value object. selectedoption = { elem: selectedoption.substring(0, selectedoption.indexof('=')), value: selectedoption.substring(selectedoption.indexof('=') + 1) }; // dropdown domelement in page can change selectedindex. dropdown = document.queryselector('#' + selectedoption.elem); var index = 0, selectedindex = 0; // loop through dropdown's children element find right one. dropdown.childnodes.foreach(function(elem, i) { // make sur domelement nodes only. if (elem.nodetype == 1) { if (elem.value === selectedoption.value) { elem.setattribute("selected", "selected"); selectedindex = index; return true; } index++; } }); // have right index, let's make sure it's selected. dropdown.selectedindex = selectedindex; }
<select id="firstdropdown"> <option value="nj">new jersey</option> <option value="ny">new york</option> <option value="la">los angeles</option> </select>
running snippet select new york city.
Comments
Post a Comment