Suresh
Suresh

Reputation: 39501

how to change the value in the text box to current date depending up on value selected in the dropdown box in javascript

hi I have a two boxes, a 'parent' select box and a 'child' text box. how can i change values that are inside the 'child' text box to current date, depend on what is selected in the 'parent' select box.

Upvotes: 0

Views: 341

Answers (3)

Tim Down
Tim Down

Reputation: 324567

Here's some code, which has the multiple advantages of not requiring jQuery and working before the document finishes loading:

<form>
  <select name="sel1" onchange="this.form.elements['txt1'].value = this.options[this.selectedIndex].text;">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
  </select>

  <input type="text" name="txt1">
</form>

Upvotes: 0

rahul
rahul

Reputation: 187040

  1. Wire an onchange event for the select box.
  2. Retrieve the selected value
  3. Assign the selected value to the text box

If you can use jQuery (which I suggest)

<script>
    $(document).ready ( function () {
        $("#sel1").change ( function () {
            $("#txt1").val ( $(this).val() );
        });
    });
</script>
<select id="sel1">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
</select>

<input type="text" id="txt1" />

Upvotes: 2

Sorin
Sorin

Reputation: 2288

I won't give you cde, just how to do it

The Select box has Text and Value properties for each Item, also the Select has a javascript Event that tells you when the SelectedIndex has changed
On that Event you should read the Value from the SelectedItem and write it to the TextBox

Upvotes: 0

Related Questions