2

Something is eluding me ... it seems obvious, but I can't quite figure it out.

I want to add/remove a couple of HTML controls to a page (plain old html) when a user changes value of a dropdown list. An example is to add or remove a "number of guests in this room" textbox for each (of a number) of rooms requested ...

So if a user selects:

1 room, there is one text box

2 rooms, there are two text boxes

3 rooms, three text boxes

back to 2 rooms, two text boxes

and so on ...

3 Answers 3

4

Using straight DOM and Javascript you would want to modify the InnterHtml property of a DOM object which will contain the text boxes...most likely a div. So it would look something like:

var container = document.getElementById("myContainerDiv");
var html;
for(var i = 0; i < selectedRooms; i++)
{
    html = html + "<input type=text ... /><br />";
}
container.innerHtml = html;

Using a Javascript library like jQuery could make things slightly easier:

var html;
for(var i = 0; i < selectedRooms; i++)
{
    html = html + "<input type=text ... /><br />";
}

$("#myContainerDiv").append(html);
3
  • Another recommendation for jQuery :) Commented Nov 9, 2008 at 2:45
  • Ha! ... I get it ... thanks for kicking loose cobwebs and fuzz.
    – brmore
    Commented Nov 9, 2008 at 2:47
  • No problem...I'm a little shocked myself that it was that fresh on a Saturday night :)
    – ckramer
    Commented Nov 9, 2008 at 3:53
3

for your select list of rooms, add an onchange event handler that will show/hide the text boses.

<script>
  //swap $ with applicable lib call (if avail)
  function $(id){
    return document.getElementById(id);
  }
  function adjustTexts(obj){
    var roomTotal = 4;
    var count = obj.selectedIndex;
    //show/hide unrequired text boxes...
    for(var i=0;i<roomTotal;i++){
      if(i < count){
        $('room'+ (i+1)).style.display = 'block';
      } else {
        $('room'+ (i+1)).style.display = 'none';
      }
    }
  }
</script>


<select name="rooms" onchange="adjustTexts(this);">
  <option>1</option>
  <option>2</option>
  <option>3</option>
  <option>4</option>
</select>

<div id="room1">
  <label>Room 1</label>:<input type="text" name="room1text"/>
</div>

<div id="room2" style="display:none;">
  <label>Room 2</label>:<input type="text" name="room2text"/>
</div>

<div id="room3" style="display:none;">
  <label>Room 3</label>:<input type="text" name="room3text"/>
</div>

<div id="room4" style="display:none;">
  <label>Room 4</label>:<input type="text" name="room4text"/>
</div>
1

given HTML:

<select name="rooms" id="rooms">
  <option value="1">1 room</option>
  <option value="2">2 rooms</option>
  <option value="3">3 rooms</option>
</select>
<div id="boxes"></div>

javascript:

document.getElementById('rooms').onchange = function() {
  var e = document.getElementById('boxes');
  var count = parseInt(document.getElementById('rooms').value);
  e.innerHTML = '';

  for(i = 1; i <= count; i++) {
    e.innerHTML += 'Room '+i+' <input type="text" name="room'+i+'" /><br />';
  } 
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.