1
$(document).ready(function(){ $("#changeText").click(function() { $("#textBox").html("text text"); }); });

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

1
  • I'm bothered by the "#textBox". Is it a textbox? If it is then you can't use html() or append() on that... Commented Aug 9, 2010 at 9:09

2 Answers 2

4

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()

Sign up to request clarification or add additional context in comments.

3 Comments

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: I'm afraid I don't understand. Please try to clarify
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).
0

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>");
});

2 Comments

why bring another div into play?
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.

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.