Reputation: 635
I have this HTML code at the moment:
Theme
<ul id="dropdown">
<li> Choose theme
<ul>
<li id="stylesheet1" > <a href="#"> Default </a></li>
<li id="stylesheet2" > <a href="#"> Theme 1 </a></li>
<li id="stylesheet3" > <a href="#"> Theme 2 </a></li>
<li id="stylesheet4" > <a href="#"> Theme 3 </a></li>
</ul>
</li>
</ul>
And in a separate javascript document I have this code:
function initate()
{
document.getElementById("myList").onchange = function() {
var sheet=document.getElementById("myList").value;
if(sheet=="one"){
setActiveStyleSheet("theme1");
}
else if(sheet=="two"){
setActiveStyleSheet("theme2");
}
else if(sheet="three"){
setActiveStyleSheet("theme3");
}
else{
setActiveStyleSheet("default");
}
return false
};
}
window.onload = initate;
Now this works good but instead of the dropdown I'm using above I'd like to use this HTML code instead:
Select Theme
<form>
<select id="myList" >
<option id="stylesheet1">Default</option>
<option id="stylesheet2">Theme 1</option>
<option id="stylesheet3">Theme 2</option>
<option id="stylesheet4">Theme 3</option>
</select>
<form>
As before I would like to have my event handlers in my separate javascript document. I've tried to replace my javascript into these kind of functions:
document.getElementById("stylesheet1").onchange = function() {
setActiveStyleSheet("default");
return false
};
But it doesn't work. How do you correct call functions this way?
Upvotes: 3
Views: 31157
Reputation: 5687
<select id="myList" >
<option id="stylesheet1" value="default">Default</option>
<option id="stylesheet2" value="one">Theme 1</option>
<option id="stylesheet3" value="two">Theme 2</option>
<option id="stylesheet4" value="three">Theme 3</option>
</select>
document.getElementById("myList").onchange = function() {
var sheet=document.getElementById("myList").value;
if(sheet=="one"){
setActiveStyleSheet("theme1");
}
else if(sheet=="two"){
setActiveStyleSheet("theme2");
}
else if(sheet=="three"){
setActiveStyleSheet("theme3");
}
else{
setActiveStyleSheet("default");
}
return false
};
Upvotes: 4
Reputation: 2624
Demo: http://jsfiddle.net/zYMFa/
Use values for options and handle change event for select. The value property of select gets value of the selected option, so you can use this.value in change function.
<form>
<select id="myList" >
<option value="default">Default</option>
<option value="theme1">Theme 1</option>
<option value="theme2">Theme 2</option>
<option value="theme3">Theme 3</option>
</select>
<form>
document.getElementById("myList").onchange = function() {
setActiveStyleSheet(this.value);
return false
};
Upvotes: 6