Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6889467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:09:35+00:00 2026-05-27T06:09:35+00:00

Ok, so here is my code. I am trying to select a picklist value

  • 0

Ok, so here is my code. I am trying to select a picklist value using Javascript, from PHP (I do not want a direct method to do this with PHP, as this won’t work for my particular program)

Here’s the code:

ECHO '<script type="text/javascript">
    var pl = document.getElementById("cultpicklist");
 pl.options[37].selected = true;
 </script>';

However when I try to run this, it does not seem to work and it says pl.options[37] is undefined.

What am I doing wrong?

Note, there is a multiple select list which has an option with a value of 37.

EDIT: I seem to get this error or warning message:

Element referenced by ID/NAME in the global scope. Use W3C standard document.getElementById() instead.
admin.php?org=7()admin.php?org=7 (line 68)
pl.options[37].selected = true;

Here’s the relevant HTML:

<fieldset><label for="culture">Culture:</label>
        <select name="culture[]" multiple="multiple" id="cultpicklist"><?php
    while ($cultrow = mysql_fetch_array($rescult)) {
        ECHO '<option name="culture[]" value="'. stripslashes($cultrow['cult_id']) .'">'. stripslashes($cultrow['cult_desc']) .'</option>';
    }
    ?>
        </select></fieldset>

Here’s the generated HTML code:

<select id="cultpicklist" multiple="multiple" name="culture[]">
<option value="36" name="culture[]">test1</option>
<option value="37" name="culture[]">test2</option>
<option value="38" name="culture[]">test3</option>
<option value="39" name="culture[]">test4</option>
</select>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:09:36+00:00Added an answer on May 27, 2026 at 6:09 am

    The index of the options IS NOT the value of the option. When you use pl.options[37] you saying to JS to get the thirty-seventh option in the select and not the option with the value 37.

    Try this:

    <script type="text/javascript">
        var pl = document.getElementById("cultpicklist");
        for(var i=0; i<pl.options.length;i++) {
            if(pl.options[i].value == "") { // Between the quotes you place the comparison value!
                pl.options[i].selected = true;
            }
        }
    </script>
    

    BTW: If you already use jQuery on your page it’s more correct to use it’s functions, but if you don’t use jQuery is too much code to add to your page just to change a select value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get data from two tables. Here is my code: select p.nim,
I am trying to generate some xml with this php code here... I am
I am fiddling with this javascript code here http://jsfiddle.net/7Sd4W/4/ I am trying to get
Trying to truncate some code here and running into a problem: <script type=text/javascript> $(function()
I am trying to select specific values from a xml document using XPath. The
I'm trying to select a value in a select element. I'm using Selenium RC
I am trying to call a select statement using datamapper in sinatra. here is
I have this code here, but it's not working I just get No Process
Here's the code I'm trying to make work: <?php class database { var $connection;
I'm having a hard time trying to do this. I have this code here:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.