How to make options selected by default?

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

    #1

    How to make options selected by default?

    I have two list box (A and B), and I have two buttons to move list
    items (options)
    from this box to the other.I have a submit button to submit the form.

    I know how to move items from A to B by using Javascript, and I know
    how to use <option ... selectedto select an item.

    However, how can I select an item after the page is rendered? In other
    words, A and B have been populated with data and the page is rendered.
    I click the move items button to move other items from A to B. I want
    all items in B are selected.

    Thanks,
  • Rik Wasmus

    #2
    Re: How to make options selected by default?

    On Tue, 11 Dec 2007 18:27:27 +0100, Ming <minghuiyu@gmai l.comwrote:
    I have two list box (A and B), and I have two buttons to move list
    items (options)
    from this box to the other.I have a submit button to submit the form.
    >
    I know how to move items from A to B by using Javascript, and I know
    how to use <option ... selectedto select an item.
    >
    However, how can I select an item after the page is rendered? In other
    words, A and B have been populated with data and the page is rendered.
    I click the move items button to move other items from A to B. I want
    all items in B are selected.
    =comp.lang.java script
    Just select it just after you add the it to the b <selectwith
    javascript. Alternatively, just select every item in B in the onsubmit()
    action of your form. Be very, very aware offcourse this will break all
    functionality for those without javascript.
    --
    Rik Wasmus

    Comment

    • Steve

      #3
      Re: How to make options selected by default?


      "Ming" <minghuiyu@gmai l.comwrote in message
      news:bad68652-b509-4362-900d-ae4a3f75a46c@s1 9g2000prg.googl egroups.com...
      >I have two list box (A and B), and I have two buttons to move list
      items (options)
      from this box to the other.I have a submit button to submit the form.
      >
      I know how to move items from A to B by using Javascript, and I know
      how to use <option ... selectedto select an item.
      >
      However, how can I select an item after the page is rendered? In other
      words, A and B have been populated with data and the page is rendered.
      I click the move items button to move other items from A to B. I want
      all items in B are selected.
      simplest break-down i could think of...

      <?
      $items = isset($_REQUEST['items']) ?
      $_REQUEST['items'] :
      array();
      $myItems['A List'][] = 'Hello';
      $myItems['A List'][] = 'World';
      $myItems['B List'][] = 'B';
      ?>
      <form method="post">
      <?
      foreach ($myItems as $type =$defined)
      {
      foreach ($defined as $index =$item)
      {
      $selected = isset($items[$type][$index]) ? 'checked' : '';
      ?>
      <input name="items[<?= $type ?>][<?= $index ?>]"
      type="checkbox"
      value="<?= $item ?>"
      <?= $selected ?>
      >&nbsp;&nbsp;<? = $item ?>
      <br>
      <?
      }
      }
      ?>
      <input type="submit" value="Save And Refresh">
      </form>


      Comment

      • Steve

        #4
        Re: How to make options selected by default?


        "Steve" <no.one@example .comwrote in message
        news:e7A7j.32$% 91.17@newsfe05. lga...
        >
        "Ming" <minghuiyu@gmai l.comwrote in message
        news:bad68652-b509-4362-900d-ae4a3f75a46c@s1 9g2000prg.googl egroups.com...
        >>I have two list box (A and B), and I have two buttons to move list
        >items (options)
        >from this box to the other.I have a submit button to submit the form.
        >>
        >I know how to move items from A to B by using Javascript, and I know
        >how to use <option ... selectedto select an item.
        >>
        >However, how can I select an item after the page is rendered? In other
        >words, A and B have been populated with data and the page is rendered.
        >I click the move items button to move other items from A to B. I want
        >all items in B are selected.
        >
        simplest break-down i could think of...
        >
        <?
        $items = isset($_REQUEST['items']) ?
        $_REQUEST['items'] :
        array();
        $myItems['A List'][] = 'Hello';
        $myItems['A List'][] = 'World';
        $myItems['B List'][] = 'B';
        two things...first, based on your description, subtract the submitted items
        from your default lists...

        $myItems['A List'] = array_diff_asso c($myItems['A List']);
        $myItems['B List'] = array_diff_asso c($myItems['B List']);

        second, change the original same to be options of a list box rather than
        just the check boxes. ah screw it...give me a second to repost the answer. i
        should have read your post better to begin with!


        Comment

        Working...