0

How to created textbox dynamically , when i click add button.

after i put the input field in the text box.

how to validate the dynamically created textbox using javascript or jquery.

<html>
<head>
<title>Adding and Removing Text Boxes Dynamically</title>

<script type="text/javascript">
var intTextBox=0;
//FUNCTION TO ADD TEXT BOX ELEMENT 
function addElement(){
  intTextBox = intTextBox + 1;
  var contentID = document.getElementById('content');
  var newTBDiv = document.createElement('div');
  newTBDiv.setAttribute('id','strText'+intTextBox);
  newTBDiv.innerHTML = "Text "+intTextBox+": <input type='text' id='" + intTextBox + "'    name='" + intTextBox + "'/>"; 
  contentID.appendChild(newTBDiv);
}

  </head>
  <body>
    <p>Demo of Adding and Removing Text Box Dynamically using JavaScript</p>
    <p><a href="javascript:addElement();" >Add</a> 
       <a href="javascript:removeElement();" >Remove</a>
    </p>
    <div id="content"></div>
    <input type="button" value="submit"/>
  </body>
  </html>

after when i click submit button all textbox validation must be done....

4
  • what do you mean by "validate the textbox" ? to check that it exists ? to validate the input the user added into it ? what do you mean by "dynamically create textbox - dynamically when what event occurs ? Commented Feb 27, 2012 at 7:51
  • yes validate the dynamically created text box.... is it possible? Commented Feb 27, 2012 at 7:53
  • sure, when you create the textbox assign it with ID and then use getElementById() to make sure it exists Commented Feb 27, 2012 at 7:54
  • dynamically means when i click add button the textbox added to the form.... Commented Feb 27, 2012 at 7:56

4 Answers 4

1

Try to copy this and test. And let's tell me that is as you need.

<!DOCTYPE HTML>
<html>
<head>
<title>Adding and Removing Text Boxes Dynamically</title>

<script type="text/javascript">
var intTextBox = 0 ;//FUNCTION TO ADD TEXT BOX ELEMENT 
function addElement(){
    intTextBox += 1
  var contentID = document.getElementById('content');
  var newTBDiv = document.createElement('div');
  newTBDiv.setAttribute('id','strText'+intTextBox);
  newTBDiv.innerHTML = "Text "+intTextBox%x+": <input type='text' id='%2+ intTextBox + "'    name='" + intTextBox + "' />"; 
  contentID.appendChild(newTBDiv);
}

function removeElement(){
    var element = document.getElementById("strText"+intTextBox);
    console.log(element);
    while (element.firstChild) {
        element.removeChild(element.firstChild);
    }
    intTextBox -=1;
}
</script>
</head><body>
<p>Demo of Adding and Removing Text Box Dynamically using JavaScript</p>
<p><a href="javascript:addElement()" >Add</a> 
   <a href="javascript:removeElement();" >Remove</a>
</p>
<div id="content"></div>
<input type="button" value="submit"/>
</body>
</html>

See more at My jsFiddle

Note : working with FireFox & Chrome

Sign up to request clarification or add additional context in comments.

Comments

1

create textbox:

var textbox = $('<input></input>');
textbox.setAttr(type, 'text');

add to container:

$('#button').onclick(function() {
    $('#container').append(textbox);
});

validate:

$('#submit').onclick(function() {
   if(!$('#textbox').val()) {
      alert('input empty');
   }
});

2 Comments

this is only used for single text box.. but i ask for dynamically created text box validation..
function addElement() { intTextBox = intTextBox + 1; var contentID = document.getElementById('content'); var newTBDiv = document.createElement('div'); newTBDiv.setAttribute('id','strText'+intTextBox); newTBDiv.innerHTML = "Text "+intTextBox+": <input type='text' id='" + intTextBox + "' name='" + intTextBox + "'/>"; contentID.appendChild(newTBDiv); }
1

You could use this jQuery Validation Plugin. The demo shows that is can validate textboxes.

2 Comments

<script>function addElement() { intTextBox = intTextBox + 1; var contentID = document.getElementById('content'); var newTBDiv = document.createElement('div'); newTBDiv.setAttribute('id','strText'+intTextBox); newTBDiv.innerHTML = "Text "+intTextBox+": <input type='text' id='" + intTextBox + "' name='" + intTextBox + "'/>"; contentID.appendChild(newTBDiv); }</script>
function addSense(sense){ comment.content += sense; }
1

that's alot of questions rolled into one. You can create and insert elements in jquery with code like the following:

$('<input />', {type : 'text'}).appendTo($(body));

As far as validation is concerned, it would depend on when you want to do it. on keypress, on submit, on blur, etc. If you want a blanket validate for all inputs you'll need to delegate it to some parent element since these textboxes are created after the page is loaded:

$('body').delegate('input','keyup', function(){
    /*Do stuff*/
});

But you could also attach the validation when you create the element if it needs to be specific to the field you create.

$('<input />', {type : 'text'}).bind('keyup', function(){ 
    /*Do stuff*/
}).appendTo($(body));

I hope that steers you in the right direction. Good luck

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.