1

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>
2
  • Make sure that code is executed after the select list is created. Commented Nov 28, 2011 at 18:36
  • It should be. It's at the bottom, and only runs when a button is pressed. Commented Nov 28, 2011 at 18:43

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

You are likely putting the cart before the horse. You cannot use JavaScript to manipulate DOM elements until they've been rendered in the browser.

Put your script AFTER your HTML or check to see of the DOM is ready before running your script.

3 Comments

My script is after my HTML. This script is at the bottom of the page, after the HTML has been rendered.
Very true, ohhhh so many times did i place JS before the DOM loaded. That was before Jquery existed tho :)
OK, please post your HTML so we can take a look.
0

Why not use JQuery first of all...

$('#cultpicklist').val('myvaluetoselect');

Is MUCH easier to code, read and works instead of using bulky old school javascript...

4 Comments

Trying not to use any external scripts as I'm not sure they're allowed or desired.
@MathieuDumoulin jquery is great, but not bulky? wake up to your self
Bulky in terms of amount of code to write, sure the 31kb minified version of Jquery is quite BULKY to download but helps in making things easier. Thank you for pointing out how bad you understood my answer. It was my pleasure to enlighten you.
Hey, please no fighting? I deeply want to get this solved, not be a forum for whether plain Javascript or jQuery is better. I have heard arguments back and forth ad infinantum.
0

Using jQuery, you could still put your code in between <head></head> tags. You would use $(document).ready() thus executing your code after the whole document has rendered.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.