How make that if i push on #changeText twice or more, i get two (or more) text text
2 Answers
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()
3 Comments
lolalola
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?
jAndy
@lolalola: I'm afraid I don't understand. Please try to clarify
lolalola
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).
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
jAndy
why bring another div into play?
Artem Barger
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.
"#textBox". Is it a textbox? If it is then you can't usehtml()orappend()on that...