?
Solved

Select Statement with Javascript

Posted on 2007-11-29
12
Medium Priority
?
531 Views
Last Modified: 2012-05-05
I am writing an ASP page that needs to provide a definition depending on what the user selects.

Example -

<select name="fruit">
<option value="">Please Select</option>
 <option value="Apple">Apple</option>
 <option value="Orange">Orange</option>
 </select>

Depending on which fruit they select, a little window pops up with definition. How do I do this?
0
Comment
Question by:JodyFischer
[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
  • 5
  • 4
  • 3
12 Comments
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20376095
JodyFischer,

You can do that with something like ...

<script type="text/javascript">
function showDef(val) {
    switch (val) {
        case "Apple":
            alert('A red fruit.');
            break;
        case "Orange":
            alert('An orange colored fruit.');
            break;
    }
}
</script>

The code above would go in the head section of your html.  The select tag would become ...

<select name="fruit" onchange="showDef(this.value);">

Let me know if you have any questions or need more information.

b0lsc0tt
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20376104
JodyFischer,

I used an "alert" but you could make a popup or have the definition appear in some "popup div" on the page.  The alert part can easily be changed to something else but the rest will show you how to do the Javascript part.

Let me know if you have a question.

b0lsc0tt
0
 
LVL 19

Expert Comment

by:erikTsomik
ID: 20376148
try something like this
function vag (obj) {
if (obj.value)=='Apple' {
alert ('Apple');
}
if (obj.value)=='Orange'{
alert ('Orange');
}
}
<select name="fruit" onchange="vag (this);">
<option value="">Please Select</option>
 <option value="Apple">Apple</option>
 <option value="Orange">Orange</option>
 </select>
0
Independent Software Vendors: 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!

 

Author Comment

by:JodyFischer
ID: 20376159
It works as an alert, but I need it to be a small second browser. Can you show me how to do that instead?
0
 
LVL 19

Expert Comment

by:erikTsomik
ID: 20376167
what do you mean by that
0
 

Author Comment

by:JodyFischer
ID: 20376227
You wrote

I used an "alert" but you could make a popup or have the definition appear in some "popup div" on the page.  The alert part can easily be changed to something else but the rest will show you how to do the Javascript part.

How do you make it a pop up (so it looks like a second browser window) instead of a grey box. I need to be able to format and have multiple lines on what comes up.
0
 
LVL 54

Accepted Solution

by:
b0lsc0tt earned 2000 total points
ID: 20376237
Sure ...

<script type="text/javascript">
function showDef(val) {
    switch (val) {
        case "Apple":
            makeWin('A red fruit.');
            break;
        case "Orange":
            makeWin('An orange colored fruit.');
            break;
    }
}
function makeWin(str) {
    var w = window.open('','window1');
    w.document.write("<html><head><title></title></head>");
    w.document.write("<body>" + str + "</body></html>");
    w.document.close;
}
</script>

Of course add more html as needed but that will show you the general idea.  Let me know if you have a question.
0
 
LVL 19

Expert Comment

by:erikTsomik
ID: 20376242
then you can try something like this
http://www.malsup.com/jquery/block/#page
0
 

Author Comment

by:JodyFischer
ID: 20376446
I am so sorry to be a pain, but it's giving me an error - Access Denied

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
      <title>Untitled</title>
<script type="text/javascript">
function showDef(val) {
    switch (val) {
        case "Apple":
            makeWin('A red fruit.');
            break;
        case "Orange":
            makeWin('An orange colored fruit.');
            break;
    }
}
function makeWin(str) {
    var w = window.open('','window1');
    w.document.write("<html><head><title></title></head>");
    w.document.write("<body>" + str + "</body></html>");
    w.document.close;
}
</script>

</head>

<body>


<select name="fruit" onchange="showDef(this.value);">
<option value="">Please Select</option>
 <option value="Apple">Apple</option>
 <option value="Orange">Orange</option>
 </select>
</body>
</html>
0
 

Author Comment

by:JodyFischer
ID: 20376531
I closed everything and it's working great. Thank you so much.
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20376625
Your welcome!  I'm glad I could help.  Thanks for the grade, the points and the fun question.

bol
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20376677
By the way, to improve the script and handle if the window already exists you could use ...

<script type="text/javascript">
var w = null;
function showDef(val) {
    switch (val) {
        case "Apple":
            makeWin('A red fruit.');
            break;
        case "Orange":
            makeWin('An orange colored fruit.');
            break;
    }
}
function makeWin(str) {
    if (w== null || w.closed) {
        w = window.open('','window1');
    }
    w.document.write("<html><head><title></title></head>");
    w.document.write("<body>" + str + "</body></html>");
    w.document.close;
}
</script>

That change just makes sure the popup window doesn't exist or has been closed.

bol
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
Suggested Courses

777 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