tags:

views:

41

answers:

2

Hi,

<script type="text/javascript">
$(document).ready(function(){
    $("#changeText").click(function() {
        $("#textBox").html("<div>text text</div>");
    });
});
</script>

How make that if i push on #changeText twice or more, i get two (or more) text text

+4  A: 

don't use .html() which overwrites the entire innerHTML structure.

To append new html markup use .append() or .appendTo()

$(document).ready(function(){
  $("#changeText").click(function() {
      $("#textBox").append("<div>text text</div>");
  });
});

or in a more jQuery'ish chaining way:

$('<div>text text</div>').appendTo($('#textBox')); // .css().fadeOut().animate().etc()

Ref.: .append(), .appendTo()

jAndy
Thank you very much, But why new <div> put no't in #textBox start. Always when i make second (or more) make space before new div?
lolalola
@lolalola: I'm afraid I don't understand. Please try to clarify
jAndy
Very sorry for my English language. When insert new "<div>text text</div>" befor this tag emerge space the same size as the previous been div. (I insert all div in one row with float: left).
lolalola
A: 

You need to create div inside #textBox and then

$("#changeText").click(function() {
    $("#textBox div").append("text");
});

EDIT:

$("#changeText").click(function() {
    if ( $("#textBox div").length()>0)
          $("#textBox div").append("text");
    else
         $("#textBox").append("<div>text</div>");
});
Artem Barger
why bring another div into play?
jAndy
and why insert each time new one? he was interested inserting each time new "text", but not the "text" inside new div. anyway could be easily changed and extend to whatever you like. As a main direction solution correct.
Artem Barger