/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Multiple use select box.

The following does not work, but I’m looking for the proper syntax so that it will.

[code=php]
<html>
<head>
<title>OO SelectBox</title>
<script type=”text/javascript”>
var SelectNames = new Array
( ”, “Alley, Gasoline”, “Boop, Betty”, “Cow, Clarabel”, “Duck, Donald”, “Fudd, Elmer”);
var SelectSites = new Array
( “”, “Work”, “Home”, “Cell”, “Fax”);

function SelectList(idInfo,listInfo) {
document.write(‘<select id=”‘+idInfo+'” name=”‘+idInfo+'”>’);
for (var i=0; i<listInfo.length; i++) {
document.write(‘<option value=”‘+listInfo[i]+'” />’+listInfo[i]);
}
document.write(‘</select>’);
}
function getSelectList(idInfo,listInfo) {
fnd = -1;
for (var i=0; i<listInfo.length; i++) {
var sel = document.getElementById(idInfo);
if (sel.selectedIndex == i) { fnd = i; }
}
var str = “”;
if (fnd == -1) { str = “”; }
else { str = document.getElementById(idInfo).options[fnd].value; }
return str;
}
function Validate() {
document.getElementById(‘Requestor’).value=getSelectList(‘Requestors’,’SelectNames’).value;
document.getElementById(‘RequestorSite’).value=getSelectList(‘RequestorSites’,’SelectSites’).value;
document.getElementById(‘Substitute’).value=getSelectList(‘Substitutes’,’SelectNames’).value;
document.getElementById(‘SubstituteSite’).value=getSelectList(‘SubstituteSites’,’SelectSites’).value;
var R = document.getElementById(‘Requestor’).value;
var S = document.getElementById(‘Substitute’).value;
if (R == S) { alert(‘Can not be same person’); return false; }
else { alert(‘OK’); return true; }
}
</script>
</head>
<body>
Requestor: <input type=”hidden” id=”Requestor” value=””>
<script type=”text/javascript”>
SelectList(‘Requestors’,’SelectNames’);
</script>
Requestor Site: <input type=”hidden” id=”RequestorSite” value=””>
<script type=”text/javascript”>
SelectList(‘RequestorSites’,’SelectSites’);
</script>

Substitute: <input type=”hidden” id=”Substitute” value=””>
<script type=”text/javascript”>
SelectList(‘Substitutes’,’SelectNames’);
</script>
Substitute Site: <input type=”hidden” id=”SubstituteSite” value=””>
<script type=”text/javascript”>
SelectList(‘SubstitutesSites’,’SelectSites’);
</script>

<button onClick=”Validate()”>Check Entries</button>
</body>
</html>
[/code]

The goal is to be able to use the functions to display and select from different array contents and stuff the selection into an ‘id’ element for later use.

What am I doing wrong OR is there a better way to do this? ?

Thanks for any thoughts on the concept.

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@ipingaJul 27.2007 — well... i only looked at it for a minute, but i noticed 3 things...

  • 1. you dont need the quotation marks around variables like SelectNames for example. What this means:


  • change this:

    <i>
    </i>SelectList('Requestors','SelectNames');


    to this:

    <i>
    </i>SelectList('Requestors',SelectNames);


    whenever you see it...

  • 2. the for loop in the SelectList function is wrong, because you're using the SelectNames array's length instead of the one you want, listInfo.


  • 3. also inside the for loop in the same function, option tags should be properly closed.. so, use "<option value="test">Display Text</option>" instead of "<option value="test" />Display Text"
  • Copy linkTweet thisAlerts:
    @JMRKERauthorJul 27.2007 — Thanks, I'll give it a try and get back to you later.

    Appreciate the thoughts.
    Copy linkTweet thisAlerts:
    @JMRKERauthorJul 27.2007 — @ipinga,

    That was pretty good for only one minute.

    Now only have one error being reported:

    in this line of the 'getSelectList(idInfo,listInfo)' function.
    [code=php]
    if (sel.selectedIndex == i) { fnd = i; }
    [/code]

    and it says: 'sel has no properties'.

    Can I have one more minute please?

    Here is the modified script.
    [code=php]
    <html>
    <head>
    <title>OO SelectBox</title>
    <script type="text/javascript">
    var SelectNames = new Array
    ( '', "Alley, Gasoline", "Boop, Betty", "Cow, Clarabel", "Duck, Donald", "Fudd, Elmer");
    var SelectSites = new Array
    ( "", "Work", "Home", "Cell", "Fax");

    function SelectList(idInfo,listInfo) {
    document.write('<select id="'+idInfo+'" name="'+idInfo+'">');
    for (var i=0; i<listInfo.length; i++) {
    document.write('<option value="'+listInfo[i]+'" />'+listInfo[i]);
    }
    document.write('</select>');
    }
    function getSelectList(idInfo,listInfo) {
    fnd = -1;
    for (var i=0; i<listInfo.length; i++) {
    var sel = document.getElementById(idInfo);
    if (sel.selectedIndex == i) { fnd = i; }
    }
    var str = "";
    if (fnd == -1) { str = ""; }
    else { str = document.getElementById(idInfo).options[fnd].value; }
    return str;
    }
    function Validate() {
    document.getElementById('Requestor').value=getSelectList('Requestors',SelectNames).value;
    document.getElementById('RequestorSite').value=getSelectList('RequestorSites',SelectSites).value;
    document.getElementById('Substitute').value=getSelectList('Substitutes',SelectNames).value;
    document.getElementById('SubstituteSite').value=getSelectList('SubstituteSites',SelectSites).value;
    var R = document.getElementById('Requestor').value;
    var S = document.getElementById('Substitute').value;
    if (R == S) { alert('Can not be same person'); return false; }
    else { alert('OK'); return true; }
    }
    </script>
    </head>
    <body>
    Requestor: <input type="hidden" id="Requestor" value="">
    <script type="text/javascript"> SelectList('Requestors',SelectNames);</script>
    Requestor Site: <input type="hidden" id="RequestorSite" value="">
    <script type="text/javascript"> SelectList('RequestorSites',SelectSites);</script>

    Substitute: <input type="hidden" id="Substitute" value="">
    <script type="text/javascript"> SelectList('Substitutes',SelectNames);</script>
    Substitute Site: <input type="hidden" id="SubstituteSite" value="">
    <script type="text/javascript"> SelectList('SubstitutesSites',SelectSites);</script>

    <button onClick="Validate()">Check Entries</button>
    </body>
    </html>
    [/code]
    Copy linkTweet thisAlerts:
    @ipingaJul 27.2007 — ok, now i looked at the getSelectList function and it was kinda weird... why dont u try this instead? I changed the validate() and getSelectList() functions. (changed this last one to getSelection()).

    <i>
    </i>function getSelection(idInfo) {
    var sel = document.getElementById(idInfo);
    return sel.options[sel.selectedIndex].value;
    }
    function Validate() {
    document.getElementById('Requestor').value=getSelection('Requestors');
    document.getElementById('RequestorSite').value=getSelection('RequestorSites');
    document.getElementById('Substitute').value=getSelection('Substitutes');
    document.getElementById('SubstituteSite').value=getSelection('SubstituteSites');
    var R = document.getElementById('Requestor').value;
    var S = document.getElementById('Substitute').value;
    if (R == S) { alert('Can not be same person'); return false; }
    else { alert('OK'); return true; }
    }


    It should work ?

    EDIT: oh and by the way, theres a type towards the end of your code. Instead of 'SubstitutesSites' it should read 'SubstituteSites'

    EDIT 2: "type" should read "typo" in the previous edit... wow...

    Also, i would suggest changing the line:

    "document.write('<option value="'+listInfo[i]+'" />'+listInfo[i]);"

    to

    "document.write('<option value="'+listInfo[i]+'">'+listInfo[i]+'</option>');"
    Copy linkTweet thisAlerts:
    @JMRKERauthorJul 27.2007 — @ipinga,

    Looks like my lightning fast fingers got ahead of my brain again. :eek:

    It's supper time here, so I'll be back after awhile when I can test the changes. ?

    Thanks for your 2 minutes! ?
    Copy linkTweet thisAlerts:
    @JMRKERauthorJul 27.2007 — Perfect @ipinga.

    I see the reason for the 'getSelection' function change. Much more elegant!

    I thought I needed to pass the array during the design phase to match the set-up,

    but now I see that the array name is only needed for the creation of the select box.

    Thank you very much.

    For others, here is the final script.
    [code=php]
    <html>
    <head>
    <title>OO SelectBox</title>
    <script type="text/javascript">
    var SelectNames = new Array
    ( '', "Alley, Gasoline", "Boop, Betty", "Cow, Clarabel", "Duck, Donald", "Fudd, Elmer");
    var SelectSites = new Array
    ( "", "Work", "Home", "Cell", "Fax");

    function SelectList(idInfo,listInfo) {
    document.write('<select id="'+idInfo+'" name="'+idInfo+'">');
    for (var i=0; i<listInfo.length; i++) {
    document.write('<option value="'+listInfo[i]+'" />'+listInfo[i]+'</option>');
    }
    document.write('</select>');
    }
    function getSelection(idInfo) {
    var sel = document.getElementById(idInfo);
    return sel.options[sel.selectedIndex].value;
    }
    function Validate() {
    document.getElementById('Requestor').value=getSelection('Requestors');
    document.getElementById('RequestorSite').value=getSelection('RequestorSites');
    document.getElementById('Substitute').value=getSelection('Substitutes');
    document.getElementById('SubstituteSite').value=getSelection('SubstituteSites');
    var R = document.getElementById('Requestor').value;
    var S = document.getElementById('Substitute').value;
    if (R == S) { alert('Can not be same person'); return false; }
    else { alert('OK'); return true; }
    }
    </script>
    </head>
    <body>
    Requestor: <input type="hidden" id="Requestor" value="">
    <script type="text/javascript"> SelectList('Requestors',SelectNames);</script>
    Requestor Site: <input type="hidden" id="RequestorSite" value="">
    <script type="text/javascript"> SelectList('RequestorSites',SelectSites);</script>

    Substitute: <input type="hidden" id="Substitute" value="">
    <script type="text/javascript"> SelectList('Substitutes',SelectNames);</script>
    Substitute Site: <input type="hidden" id="SubstituteSite" value="">
    <script type="text/javascript"> SelectList('SubstituteSites',SelectSites);</script>

    <button onClick="Validate()">Check Entries</button>
    </body>
    </html>
    [/code]
    ×

    Success!

    Help @JMRKER spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 4.29,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...