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);
Sign up to request clarification or add additional context in comments.

3 Comments

Another recommendation for jQuery :)
Ha! ... I get it ... thanks for kicking loose cobwebs and fuzz.
No problem...I'm a little shocked myself that it was that fresh on a Saturday night :)
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>

Comments

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 />';
  } 
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.