Javascript DOM How to - Add elements to container and enlarge








Question

We would like to know how to add elements to container and enlarge.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-2.0.2.js'></script>
<style type='text/css'>
.something {<!--from   www  .j  a v a  2 s  .  c o m-->
  background-color: #red;
  padding: 10px;
  position: relative;
  border: 2px solid #000;
}

.something:before {
  content: " ";
  display: block;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  border: 2px solid #blue;
}
</style>
</head>
<body>
  <div id="container">
    <span class="something"> label: <span id="count">20</span>
    </span>
  </div>
  <br/><br/><br/>
  <div>
    <button>increase count</button>
  </div>
  <script type='text/javascript'>
$('button').click(function() {
    var temp = $(".something");
    $(".something").remove();
    var count = $('#count', temp);
    count.html(count.html() +"asdf");
    $("#container").append('<span class="something">'+temp.html() + '</span>');
});

</script>
</body>
</html>

The code above is rendered as follows: