Solved

jquery detect textbox exists

Posted on 2014-07-18
5
315 Views
Last Modified: 2014-07-18
http://jsfiddle.net/sevensnake/2NLMc/5/

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.
0
Comment
Question by:sevensnake77
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 53

Accepted Solution

by:
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

  alert($('textarea').length);

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

Author Comment

by:sevensnake77
ID: 40204676
can you show me in my example.
0
 
LVL 53

Expert Comment

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

Author Closing Comment

by:sevensnake77
ID: 40204702
tks
0
 
LVL 53

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.
http://jsfiddle.net/x63eb/2/
$("#addButton").click(function () {
    var counter = $('textarea').length +1
    if (counter > 3) {
        alert("Only 3 Comments are allowed");
        return false;
    }
    $('<div/>', {
        'ID': 'CommentBoxDiv' + counter
    }).html(
    $('<label/>').html('Comment #' + counter + ' : '))
        .append($('<textarea  cols="40" rows="6">').attr({
        'id': 'Comments' + counter,
        'name': 'Comments' + counter,
        'style': 'margin-top:10px; margin-bottom:10px'
    }))
        .appendTo('#CommentBoxesGroup')
    counter++;
});

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

Open in new window

0

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

687 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