Entering text into a div element using id and jquery?

I kept thinking about how to do this, but most of the methods seem too long or complicated. I have a button and an invisible div, when I click the button, I want the text to be written to the DIV using jquery.

Say this is my html:

<button id="buttonid"></button>

and div:

<div id="invisible"></div>

My jquery will start something like this?

$(document).ready(function(){
    $("#buttonid").click(function(){
       //WHAT COMES HERE? TO ADD TEXT TO #invisible ?
    });
});
+3
source share
3 answers
$("#buttonid").click(function(){
     $("#invisible").text("your text").show();
});

Notes:

  • I added .show(), considering that the div starts as invisible
  • Use .html()instead .text()if you plan to embed HTML markup in place of plain text.
+5
source

If you just want to change the text / html div, use $.text()or $.html().

$(document).ready(function(){
    $("#buttonid").on("click", function(){
       $("#invisible").html('Foo');
    });
});

, - , , $.show() $.fadeIn() ..:

$(document).ready(function(){
  $("#buttonid").on("click", function(){
    $("#invisible").html('Foo').fadeIn();
  });
});
+4

, div , :

$("#invisible").append("your text");

$("#invisible").text("your text");
//OR
$("#invisible").html("your text");
0

All Articles