Frustrating select box problem

Discussion in 'Javascript' started by John Burns, Apr 18, 2005.

  1. John Burns

    John Burns Guest

    Hi,
    I'm trying to get javascript to change the displayed value in a combo box
    (one of the <select type things) from a child window. I can get it to
    control check boxes and text boxes but the select box is causing me real
    problems.

    Form name is newcert, selectbox name is loadcity

    window.opener.document.newcert.loadcity.value = 'test';

    When run, this changes the displayed option to blank. Is .value not the
    option to change the displayed test in this select box???

    Any ideas?
    John Burns, Apr 18, 2005
    #1
    1. Advertising

  2. John Burns wrote:

    [Select]

    > window.opener.document.newcert.loadcity.value = 'test';


    You need to change the selectedIndex property or one option's
    selected property. If you only know the value of the option you
    want to select, but not it's index, you have to iterate the
    options like an array. Quickhack:

    var oMyForm, oMySelect, aMyOptions, i;
    if (opener
    && !opener.closed
    && (oMyform = opener.document.forms['newcert'])
    && (oMySelect = oMyForm.elements['loadcity'])
    && (aMyOptions = oMySelect.options)
    ) {
    for (i=0; i<aMyOptions.length; i++) {
    if (aMyOptions.value == "test") {
    oMySelect.selectedIndex = i;
    break;
    }
    }
    }

    ciao, dhgm
    Dietmar Meier, Apr 18, 2005
    #2
    1. Advertising

  3. John Burns

    J. Baute Guest

    instead of setting the value of the current item, you need to select
    the required already existing item in the combo box instead,

    this is done by setting the options "selected" property to true, which
    will then automatically select this option (duh!) :)

    so if you know the index of the item to select, you can do this:

    window.opener.document.newcert.loadcity.options[itemIndex].selected =
    true;

    otherwise you'll have to loop through the options collection, and set
    the selected property to true if you find a matching value,

    hope this helps,
    J.
    J. Baute, Apr 18, 2005
    #3
  4. John Burns

    John Burns Guest

    Thanks so far.
    The item I was trying to get in the list, wasn't in the list at the time, it
    was a new item.
    I guess this makes it a little more difficult?
    John Burns, Apr 18, 2005
    #4
  5. John Burns wrote:

    > The item I was trying to get in the list, wasn't in the list at the
    > time, it was a new item.
    > I guess this makes it a little more difficult?


    Not really. You can simply add an option to the options collection
    (like adding an item to an array) if it doesn't exist. Another
    quickhack:

    function selectOrAddOption(oSelect, sText, sValue) {
    var oOptions, nOptions, oCurrentOption, i;
    if (oSelect
    && (oOptions = oSelect.options)
    ) {
    nOptions = oOptions.length;
    for (i=0; i<nOptions; i++) {
    oCurrentOption = aMyOptions;
    if (oCurrentOption.value == sValue
    && oCurrentOption.text == sText
    ) {
    oMySelect.selectedIndex = i;
    break;
    }
    }
    if (i == nOptions) {
    oOptions = new Option(sText, sValue);
    oMySelect.selectedIndex = i;
    }
    }
    }

    var oMyForm, oMySelect, aMyOptions, i;
    if (opener
    && !opener.closed
    && (oMyform = opener.document.forms['newcert'])
    && (oMySelect = oMyForm.elements['loadcity'])
    ) {
    selectOrAddOption(oMySelect, "test", "42");
    }

    ciao, dhgm
    Dietmar Meier, Apr 18, 2005
    #5
  6. John Burns

    Matt Kruse Guest

    Dietmar Meier wrote:
    > if (i == nOptions) {
    > oOptions = new Option(sText, sValue);
    > oMySelect.selectedIndex = i;


    This will cause an error in IE.
    You cannot create new options and append them to a select list in any other
    'window' object but your own.

    So, you need to write a function in the opener window which will accept
    parameters and add the new option for you. Then, call that function from the
    popup window with the text and value arguments.

    --
    Matt Kruse
    http://www.JavascriptToolbox.com
    Matt Kruse, Apr 18, 2005
    #6
  7. Matt Kruse wrote:

    >> if (i == nOptions) {
    >> oOptions = new Option(sText, sValue);
    >> oMySelect.selectedIndex = i;


    > This will cause an error in IE.
    > You cannot create new options and append them to a select list in any
    > other 'window' object but your own.


    There's a trick that makes it work in IE too:

    function selectOrAddOption(oWindow, oSelect, sText, sValue) {
    var oOptions, nOptions, oCurrentOption, i;
    if (oSelect
    && (oOptions = oSelect.options)
    ) {
    nOptions = oOptions.length;
    for (i=0; i<nOptions; i++) {
    oCurrentOption = oOptions;
    if (oCurrentOption.value == sValue
    && oCurrentOption.text == sText
    ) {
    oSelect.selectedIndex = i;
    break;
    }
    }
    if (i == nOptions) {
    try {
    oOptions = new Option(sText, sValue);
    oSelect.selectedIndex = i;
    }
    catch(e) {
    try {
    oOptions = oWindow.Option(sText, sValue);
    oSelect.selectedIndex = i;
    }
    catch(e) {
    }
    }
    }
    }
    }
    function testit() {
    var oMyForm, oMySelect;
    if (opener
    && !opener.closed
    && (oMyForm = opener.document.forms['newcert'])
    && (oMySelect = oMyForm.elements['loadcity'])
    ) {
    selectOrAddOption(opener, oMySelect, "test", "42");
    }
    }

    ciao, dhgm
    Dietmar Meier, Apr 19, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Simon Harvey
    Replies:
    1
    Views:
    317
    Guest
    Apr 2, 2004
  2. michael
    Replies:
    0
    Views:
    423
    michael
    May 1, 2004
  3. Bit byte
    Replies:
    1
    Views:
    319
    mlimber
    Apr 17, 2006
  4. James

    Server Wide Problem (frustrating)

    James, Aug 15, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    270
    James
    Aug 15, 2007
  5. palmiere
    Replies:
    1
    Views:
    384
    Erwin Moller
    Feb 9, 2004
Loading...

Share This Page