TRR
TRR

Reputation: 1643

Insert breakline after textbox using jquery

I am trying to insert a breakline (<br />) after every textbox on click of a button using the below code :

HTML:

<input type="text" id="text1" /><b>This is test</b>
<br />
<button>New line</button>

Jquery:

$(document).ready(function(){
$("button").click(function(){
$("#text1").append("<br />");
});
});

I am getting the following error in the console

Message: Object doesn't support this property or method

Please let me know how I can achieve this.

Upvotes: 1

Views: 1300

Answers (3)

GautamD31
GautamD31

Reputation: 28753

Try with this

$(document).ready(function(){
   $("button").click(function(){
   $("#text1").after("<br />");
  });
});

Upvotes: 1

David
David

Reputation: 2065

use after() instead of append.

http://api.jquery.com/after/

http://jsfiddle.net/66ERc/

Upvotes: 2

Alessandro Minoccheri
Alessandro Minoccheri

Reputation: 35973

Try this:

$(document).ready(function(){
$("button").click(function(){
var txt = $("text1");
txt.val( txt.val() + "\n");
});
});

Upvotes: 1

Related Questions