Frustrating select box problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Burns

    #1

    Frustrating select box problem

    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.d ocument.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?


  • Dietmar Meier

    #2
    Re: Frustrating select box problem

    John Burns wrote:

    [Select]
    [color=blue]
    > window.opener.d ocument.newcert .loadcity.value = 'test';[/color]

    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.element s['loadcity'])
    && (aMyOptions = oMySelect.optio ns)
    ) {
    for (i=0; i<aMyOptions.le ngth; i++) {
    if (aMyOptions[i].value == "test") {
    oMySelect.selec tedIndex = i;
    break;
    }
    }
    }

    ciao, dhgm

    Comment

    • John Burns

      #3
      Re: Frustrating select box problem

      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?


      Comment

      • J. Baute

        #4
        Re: Frustrating select box problem

        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.d ocument.newcert .loadcity.optio ns[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.

        Comment

        • Dietmar Meier

          #5
          Re: Frustrating select box problem

          John Burns wrote:
          [color=blue]
          > 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?[/color]

          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 selectOrAddOpti on(oSelect, sText, sValue) {
          var oOptions, nOptions, oCurrentOption, i;
          if (oSelect
          && (oOptions = oSelect.options )
          ) {
          nOptions = oOptions.length ;
          for (i=0; i<nOptions; i++) {
          oCurrentOption = aMyOptions[i];
          if (oCurrentOption .value == sValue
          && oCurrentOption. text == sText
          ) {
          oMySelect.selec tedIndex = i;
          break;
          }
          }
          if (i == nOptions) {
          oOptions[i] = new Option(sText, sValue);
          oMySelect.selec tedIndex = i;
          }
          }
          }

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

          ciao, dhgm

          Comment

          • Matt Kruse

            #6
            Re: Frustrating select box problem

            Dietmar Meier wrote:[color=blue]
            > if (i == nOptions) {
            > oOptions[i] = new Option(sText, sValue);
            > oMySelect.selec tedIndex = i;[/color]

            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



            Comment

            • Dietmar Meier

              #7
              Re: Frustrating select box problem

              Matt Kruse wrote:
              [color=blue][color=green]
              >> if (i == nOptions) {
              >> oOptions[i] = new Option(sText, sValue);
              >> oMySelect.selec tedIndex = i;[/color][/color]
              [color=blue]
              > 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.[/color]

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

              function selectOrAddOpti on(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[i];
              if (oCurrentOption .value == sValue
              && oCurrentOption. text == sText
              ) {
              oSelect.selecte dIndex = i;
              break;
              }
              }
              if (i == nOptions) {
              try {
              oOptions[i] = new Option(sText, sValue);
              oSelect.selecte dIndex = i;
              }
              catch(e) {
              try {
              oOptions[i] = oWindow.Option( sText, sValue);
              oSelect.selecte dIndex = i;
              }
              catch(e) {
              }
              }
              }
              }
              }
              function testit() {
              var oMyForm, oMySelect;
              if (opener
              && !opener.closed
              && (oMyForm = opener.document .forms['newcert'])
              && (oMySelect = oMyForm.element s['loadcity'])
              ) {
              selectOrAddOpti on(opener, oMySelect, "test", "42");
              }
              }

              ciao, dhgm

              Comment

              Working...