Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Please Help: show/hide JS + onchange + non-unique option values
Old 01-24-2008, 03:19 PM Please Help: show/hide JS + onchange + non-unique option values
seannarae's Avatar
Skilled Talker

Posts: 65
Location: san francisco, ca
Trades: 0
i've gotten *so* far using some very helpful & well-defined JS hints & tricks from this site. now its my turn to post a question for a challenge that has me stumped.

BIZ & FUNCTIONAL REQUIREMENTS I NEED TO FULFILL:
  • I need multiple select-lists, where name is mapped to a DB field.
  • Each option in each list needs a numeric value 1-9 (which a type-table will define in the DB, not my dept)
  • Each option value needs to trigger a div SHOW containing a script that describes that lists option value, which will also HIDE the current div/script.
  • Per requirements, i must build this using select-lists.
PROGRESS & SUCCESS
Using JS + CSS found on this site, I've successfully gotten the appropriate div to show/hide.

MY PROBLEM:
The only way i have found this to work is if the div id = the option value. Which means all value-to-id combos must be unique.

However, I need my option values to remain non-unique (multiple lists with identical value ranges).

Is there a way to acheive this? Using the following JS or any other JS?

DUMMY/TEST CODE DEMONSTRATING MY SCENARIO:
Each list does trigger a SHOW/HIDE of a script div. However, since the div id's are not unique, only the first set if option value ranges are listening to the trigger(s).

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>TEST PAGE</title>

<script type="text/javascript"><!--
var lastDiv = "";
function showDiv(divName) {
    // hide last div
    if (lastDiv) {
        document.getElementById(lastDiv).className = "hiddenDiv";
    }
    //if value of the box is not nothing and an object with that name exists, then change the class
    if (divName && document.getElementById(divName)) {
        document.getElementById(divName).className = "visibleDiv";
        lastDiv = divName;
    }
}
//-->
</script>

<style type="text/css" media="screen">
<!--
.hiddenDiv {
    display: none;
    }
.visibleDiv {
    display: block;
    border: 1px grey solid;
    }

-->
</style>

</head>

<body>

<form name="script_display" method="post" action="">
<table cellpadding="0" cellspacing="0" border="0" width="600">
    <tr>

        <!-- ===== START select-list column START ===== -->
        <td width="300" valign="top">
        LIST A
        <select name="selectlista" onchange="showDiv(this.value);">
            <option value="">Select</option>
            <option value="1">Display Script A1 </option>
            <option value="2">Display Script A2 </option>
            <option value="3">Display Script A3 </option>
        </select>
        <p>
        LIST B
        <select name="selectlistb" onchange="showDiv(this.value);">
            <option value="">Select</option>
            <option value="1">Display Script B1 </option>
            <option value="2">Display Script B2 </option>
            <option value="3">Display Script B3 </option>
        </select>
        <p>
        LIST C
        <select name="selectlistc" onchange="showDiv(this.value);">
            <option value="">Select</option>
            <option value="1">Display Script C1 </option>
            <option value="2">Display Script C2 </option>
            <option value="3">Display Script C3 </option>
        </select>

        </td>
        <!-- ===== END select-list column END ===== -->

        <!-- ===== START script column START ===== -->
        <td width="300" valign="top">
        <div id="1" class="hiddenDiv"><strong>This is script A1.</strong><br />  This script should only display whilst option "Display Script A1" is selected in list A</div>
        <div id="2" class="hiddenDiv"><strong>This is script A2.</strong><br />  This script should only display whilst option "Display Script A2" is selected in list A</div>
        <div id="3" class="hiddenDiv"><strong>This is script A3.</strong><br />  This script should only display whilst option "Display Script A3" is selected in list A</div>
        <div id="1" class="hiddenDiv"><strong>This is script B1.</strong><br />  This script should only display whilst option "Display Script B1" is selected in list B</div>
        <div id="2" class="hiddenDiv"><strong>This is script B2.</strong><br />  This script should only display whilst option "Display Script B2" is selected in list B</div>
        <div id="3" class="hiddenDiv"><strong>This is script B3.</strong><br />  This script should only display whilst option "Display Script B3" is selected in list B</div>
        <div id="1" class="hiddenDiv"><strong>This is script C1.</strong><br />  This script should only display whilst option "Display Script C1" is selected in list C</div>
        <div id="2" class="hiddenDiv"><strong>This is script C2.</strong><br />  This script should only display whilst option "Display Script C2" is selected in list C</div>
        <div id="3" class="hiddenDiv"><strong>This is script C3.</strong><br />  This script should only display whilst option "Display Script C3" is selected in list C</div>
        </td>
        <!-- ===== END script column END ===== -->
        
    </tr>
</table>
</form>
</body>
</html>
seannarae is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-24-2008, 05:15 PM Re: Please Help: show/hide JS + onchange + non-unique option values
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
IDs MUST be unique

and

IDs CANNOT start with a numeric.

Fix those problems first
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 01-24-2008, 05:57 PM Re: Please Help: show/hide JS + onchange + non-unique option values
seannarae's Avatar
Skilled Talker

Posts: 65
Location: san francisco, ca
Trades: 0
thanks for your reply & lack of ambiguity.

ok, fixed. see code below.

all div id's now lead with an alpha. moreover, all div id's are unique, which i wholly prefer to keep orderly what may be dozens of script divs.

however, the option values in the triggering select lists cannot be altered to match the characters of the new div id's.

is there some other way - other then the option value - to trigger the show/hide of the appropriate div id?

Code:
<script type="text/javascript"><!--
var lastDiv = "";
function showDiv(divName) {
    // hide last div
    if (lastDiv) {
        document.getElementById(lastDiv).className = "hiddenDiv";
    }
    //if value of the box is not nothing and an object with that name exists, then change the class
    if (divName && document.getElementById(divName)) {
        document.getElementById(divName).className = "visibleDiv";
        lastDiv = divName;
    }
}
//-->
</script>

<style type="text/css" media="screen">
<!--
.hiddenDiv {
    display: none;
    }
.visibleDiv {
    display: block;
    border: 1px grey solid;
    }

-->
</style>

</head>

<body>

<form name="script_display" method="post" action="">
<table cellpadding="0" cellspacing="0" border="0" width="600">
    <tr>

        <!-- ===== START select-list column START ===== -->
        <td width="300" valign="top">
        LIST A
        <select name="selectlista" onchange="showDiv(this.value);">
            <option value="">Select</option>
            <option value="1">Display Script A1 </option>
            <option value="2">Display Script A2 </option>
            <option value="3">Display Script A3 </option>
        </select>
        <p>
        LIST B
        <select name="selectlistb" onchange="showDiv(this.value);">
            <option value="">Select</option>
            <option value="1">Display Script B1 </option>
            <option value="2">Display Script B2 </option>
            <option value="3">Display Script B3 </option>
        </select>
        <p>
        LIST C
        <select name="selectlistc" onchange="showDiv(this.value);">
            <option value="">Select</option>
            <option value="1">Display Script C1 </option>
            <option value="2">Display Script C2 </option>
            <option value="3">Display Script C3 </option>
        </select>

        </td>
        <!-- ===== END select-list column END ===== -->

        <!-- ===== START script column START ===== -->
        <td width="300" valign="top">
        <div id="A1" class="hiddenDiv"><strong>This is script A1.</strong><br />  This script should only display whilst option "Display Script A1" is selected in list A</div>
        <div id="A2" class="hiddenDiv"><strong>This is script A2.</strong><br />  This script should only display whilst option "Display Script A2" is selected in list A</div>
        <div id="A3" class="hiddenDiv"><strong>This is script A3.</strong><br />  This script should only display whilst option "Display Script A3" is selected in list A</div>
        <div id="B1" class="hiddenDiv"><strong>This is script B1.</strong><br />  This script should only display whilst option "Display Script B1" is selected in list B</div>
        <div id="B2" class="hiddenDiv"><strong>This is script B2.</strong><br />  This script should only display whilst option "Display Script B2" is selected in list B</div>
        <div id="B3" class="hiddenDiv"><strong>This is script B3.</strong><br />  This script should only display whilst option "Display Script B3" is selected in list B</div>
        <div id="C1" class="hiddenDiv"><strong>This is script C1.</strong><br />  This script should only display whilst option "Display Script C1" is selected in list C</div>
        <div id="C2" class="hiddenDiv"><strong>This is script C2.</strong><br />  This script should only display whilst option "Display Script C2" is selected in list C</div>
        <div id="C3" class="hiddenDiv"><strong>This is script C3.</strong><br />  This script should only display whilst option "Display Script C3" is selected in list C</div>
        </td>
        <!-- ===== END script column END ===== -->
        
    </tr>
</table>
</form>
</body>
</html>
seannarae is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Please Help: show/hide JS + onchange + non-unique option values
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.18074 seconds with 12 queries