Alex Mojum
Alex Mojum

Reputation: 19

Html optional box selected issue

I've a html list of select item. There is a value name: other, which if selected, I want to show a text input.

The following is my code. If the user selects any option except other, the text input should hide. How do I show this text field when the user selects other from the select.

<td>
    Interest <span style="color:#F00;">*</span>
</td>
<td>
    <select name="interest" id="travel_arriveVia" onchange="showfield(this.options[this.selectedIndex].value)">
        <option value="art" <?php if ($g_interest=="art" ) echo 'selected = "selected"'; ?>>Art</option>
        <option value="litteratures" <?php if ($g_interest=="litteratures" ) echo 'selected = "selected"'; ?>>Litteratures</option>
        <option value="business" <?php if($g_interest=="business" ) echo 'selected = "selected"'; ?>>Business</option>
        <option value="internet" <?php if($g_interest=="internet" ) echo 'selected = "selected"'; ?>>Internet</option>
        <option value="other_interest" <?php if($g_interest=="internet" ) echo 'selected = "selected"'; ?>>Other</option>
    </select>

    <script type="text/javascript">
        function showfield(name) {
            if (name == 'other_interest') document.getElementById('div1').innerHTML = '<input type="text" name="other_interest" class="trother" />';
            else document.getElementById('div1').innerHTML = '';
        }
    </script>
    <div id="div1"></div>
</td>

Thanks.

Upvotes: 0

Views: 656

Answers (2)

MrCode
MrCode

Reputation: 64536

Keep the text input there but just show and hide it when the select is changed.

<select name="interest" id="travel_arriveVia" onchange="showfield(this.options[this.selectedIndex].value)">
    <option value="art" <?php if ($g_interest=="art" ) echo 'selected = "selected"'; ?>>Art</option>
    <option value="litteratures" <?php if ($g_interest=="litteratures" ) echo 'selected = "selected"'; ?>>Litteratures</option>
    <option value="business" <?php if($g_interest=="business" ) echo 'selected = "selected"'; ?>>Business</option>
    <option value="internet" <?php if($g_interest=="internet" ) echo 'selected = "selected"'; ?>>Internet</option>
    <option value="other_interest" <?php if($g_interest=="other_interest" ) echo 'selected = "selected"'; ?>>Other</option>
</select>

<input type="text" id="other_interest" name="other_interest" class="trother" style="<?php if($g_interest != 'other_interest') echo 'display:none;'; ?>" />

<script type="text/javascript">
    function showfield(name) {

        var input = document.getElementById("other_interest");
        if (name == 'other_interest') {
            input.style.display = "";
        } else {
            input.style.display = "none";
        }
    }
</script>

Upvotes: 0

Pouki
Pouki

Reputation: 1664

Instead of your div1 you can do the following :

<select name="interest" id="travel_arriveVia" onchange="showfield(this.options[this.selectedIndex].value)">
        <option value="art" <?php if ($g_interest=="art" ) echo 'selected = "selected"'; ?>>Art</option>
        <option value="litteratures" <?php if ($g_interest=="litteratures" ) echo 'selected = "selected"'; ?>>Litteratures</option>
        <option value="business" <?php if($g_interest=="business" ) echo 'selected = "selected"'; ?>>Business</option>
        <option value="internet" <?php if($g_interest=="internet" ) echo 'selected = "selected"'; ?>>Internet</option>
        <option value="other_interest" <?php if($g_interest=="internet" ) echo 'selected = "selected"'; ?>>Other</option>
</select>

<input type="text" name="other_interest" id="txtOther" class="trother" style="display: none;" />

<script>
  function showfield(name) {
        if (name == 'other_interest') document.getElementById('txtOther').style = 'display: block;';
        else document.getElementById('txtOther').style = 'display: none;';
    }
</script>

Upvotes: 0

Related Questions