javascript - add new select option with an attribute -
i want add href attribute select list (countrylist) elements:
var s = document.getelementbyid('countrylist'); var option = document.createelement("option"); option.text = "fr"; option.value = "fr"; option.setattribute('href', 'https://www.facebook.com/'); s.add(option); var option2 = document.createelement("option"); option2.text = "en"; option2.value = "us"; option2.setattribute('href', 'https://www.facebook.com/'); s.add(option2); var option3 = document.createelement("option"); option3.text = "ar"; option3.value = "ar"; option3.setattribute('href', 'https://www.google.com/'); s.add(option3); var option4 = document.createelement("option"); option4.text = "es"; option4.value = "es"; option4.setattribute('href', 'https://www.yahoo.com/'); s.add(option4); var option5 = document.createelement("option"); option5.text = "it"; option5.value = "it"; option5.setattribute('href', 'https://www.youtube.com/'); s.add(option5);
when select option should redirect me specific href address, redirects me "yahoo" , "youtube", while other elements don't redirect.
try
var s = document.getelementbyid('countrylist'); var option = document.createelement("option"); option.text = "fr"; option.value = "fr"; option.setattribute('data-href','https://www.facebook.com/') ; s.add(option); var option2 = document.createelement("option"); option2.text = "en"; option2.value = "us"; option2.setattribute('data-href','https://www.facebook.com/') ; s.add(option2); var option3 = document.createelement("option"); option3.text = "ar"; option3.value = "ar"; option3.setattribute('data-href','https://www.google.com/') ; s.add(option3); var option4 = document.createelement("option"); option4.text = "es"; option4.value = "es"; option4.setattribute('data-href','https://www.yahoo.com/') ; s.add(option4); var option5 = document.createelement("option"); option5.text = "it"; option5.value = "it"; option5.setattribute('data-href','https://www.youtube.com/') ; s.add(option5); function redirect(obj){ window.open(obj.selectedoptions[0].getattribute("data-href")) }
<select id="countrylist" onchange="redirect(this);"> <option>select country</option> </select>
Comments
Post a Comment