Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

javascript select box issue

Posted on 2007-12-06
4
Medium Priority
?
381 Views
Last Modified: 2008-02-01
I am using this solution to make my option elements disabled in IE:
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_22993394.html

Problem is that I want to redirect on some link depending on selected option.

When  I put this code on select box: onChange="window.location.href=this.options[this.selectedIndex].value;" it cannot be done because there is already used this event in code above?

So, I set that code inside script, but it applies on all select boxes on page and I want to make it available only for one of them, on these that have id named "test"

Code from oveve script:
 
window.onload = function() {
	if (document.getElementsByTagName) {
		var s = document.getElementsByTagName("select");
 
		if (s.length > 0) {
			window.select_current = new Array();
 
			for (var i=0, select; select = s[i]; i++) {
				select.onfocus = function(){ window.select_current[this.id] = this.selectedIndex; }
				select.onchange = function(){ restore(this); 
				window.location.href=this.options[this.selectedIndex].value;
				}
				
				emulate(select);
			}
		}
	}
	window.location.href=this.options[this.selectedIndex].value;
}
 
function restore(e) {
	if (e.options[e.selectedIndex].disabled) {
		e.selectedIndex = window.select_current[e.id];
	}
}
 
function emulate(e) {
	for (var i=0, option; option = e.options[i]; i++) {
		if (option.disabled) {
			option.style.color = "graytext";
		}
		else {
			option.style.color = "menutext";
		}
	}
}
 
 
This is my select box for jump menu:
 
<select name="test" id="test">
    <option value="1">1</option>
    <option value="2">2</option>
  </select>

Open in new window

0
Comment
Question by:zvucini
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 

Expert Comment

by:moomooneo
ID: 20425166
Try this:
1. move the javascript to the end of the html (always recommended);
2. Change this line of code
       var s = document.getElementsByTagName("select");
to
       var s = document.getElementsById("test");
0
 
LVL 2

Author Comment

by:zvucini
ID: 20425264
I tried, but it returns error.
0
 
LVL 5

Accepted Solution

by:
TheGD earned 2000 total points
ID: 20426799
Replace the window.onload function with the following:
window.onload = function() {
	if (document.getElementsByTagName) {
		var s = document.getElementsByTagName("select");
 
		if (s.length > 0) {
			window.select_current = new Array();
 
			for (var i=0, select; select = s[i]; i++) {
				if (select.id == "test") {
					select.onfocus = function(){ window.select_current[this.id] = this.selectedIndex; }
					select.onchange = function(){ restore(this); 
					window.location.href = this.options[this.selectedIndex].value;
					}
				
					emulate(select);
				}
			}
		}
	}
	//window.location.href=this.options[this.selectedIndex].value;
}

Open in new window

0
 
LVL 2

Author Comment

by:zvucini
ID: 20427016
Excellent!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question