jquery detect textbox exists

Posted on 2014-07-18
Last Modified: 2014-07-18

If my textarea exists when the page loads when I click to add another comment box it knows where to leave off.

for my example there is 2 already exists so when I click add another it should start at 3 not 2.
Question by:sevensnake77
  • 3
  • 2
LVL 52

Accepted Solution

Scott Fell,  EE MVE earned 500 total points
ID: 40204649
An easier way instead of a counter is to get the total number of text areas


var nextNumber = $('textarea').length + 1

Author Comment

ID: 40204676
can you show me in my example.
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 40204686
Ok Give me a minute.

Author Closing Comment

ID: 40204702
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 40204706
The only thing I did was remove your counter variable before the click function and used it to set the counter on click.   You did the rest.
$("#addButton").click(function () {
    var counter = $('textarea').length +1
    if (counter > 3) {
        alert("Only 3 Comments are allowed");
        return false;
    $('<div/>', {
        'ID': 'CommentBoxDiv' + counter
    $('<label/>').html('Comment #' + counter + ' : '))
        .append($('<textarea  cols="40" rows="6">').attr({
        'id': 'Comments' + counter,
        'name': 'Comments' + counter,
        'style': 'margin-top:10px; margin-bottom:10px'

$("#removeButton").click(function () {
    if (counter == 1) {
        alert("No more Comment boxes to remove. One will remain.");
        return false;
    $("#CommentBoxDiv" + counter).remove();

Open in new window


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
New to Angular - are these sections correct? how to do the $scope 19 47
Where do I learn ES6? 3 51
JS Plugin Chaining 2 32
Javascript Hashmap output 8 47
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now