| | |
add text box on click
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jul 2008
Posts: 13
Reputation:
Solved Threads: 0
Hello All,
I have this situation.
1. I have one Text box (say first) and add button.
2. If i click add, one text box should be added + one delete button.
3. if i click add again, second text box should be added + one delete button.
and so on..
4. on clicking any delete button, the corresponding text box should get deleted.
5. The add button should be for only first text box.
Please provide me code for this.
or only for adding textboxes, iam new to Javascript.
Thanks a lot.
I have this situation.
1. I have one Text box (say first) and add button.
2. If i click add, one text box should be added + one delete button.
3. if i click add again, second text box should be added + one delete button.
and so on..
4. on clicking any delete button, the corresponding text box should get deleted.
5. The add button should be for only first text box.
Please provide me code for this.
or only for adding textboxes, iam new to Javascript.
Thanks a lot.
i think this code will help you...
html Syntax (Toggle Plain Text)
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> <script> function generateRow() { var d=document.getElementById("div"); d.innerHTML+="<p><input type='text' name='food'>"; } </script> </head> <body> <form id="form1" name="form1" method="post" action=""> <label> <input name="food" type="text" id="food" /> </label> <p> <input name="food" type="text" id="food" /> </p> <p> <input name="food" type="text" id="food" /> </p> <div id="div"></div> <p><input type="button" value="Add" onclick="generateRow()"/></p> <p> <label> <input type="submit" name="Submit" value="Submit" /> </label> </p> </form> </body> </html>
Last edited by Shanti Chepuru; Aug 28th, 2008 at 6:55 am.
Be intelligent, But Don't try to cheat.. Be innocent But Don't get cheated..
Please feel free to modify this code to match your needs. If you have any question regarding this code you can document.write('Me on my inbox'). lol! Have a good day...
html Syntax (Toggle Plain Text)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Some Title</titles>
css Syntax (Toggle Plain Text)
<style type="text/css"> <!-- form div { margin: 4px 4px 0 0; padding: 0; } form input { margin-left: 4px; } form input[type=button] { background-color: #000000; color: silver; margin-left: 2px; } #b1 { background-color: gold; color: black; } --> </style>
javascript Syntax (Toggle Plain Text)
<script type="text/javascript"> <!-- BEGIN HIDING /* A simple demonstration on how to create and delete, textBox with an onclick event. Hope you had great time! Enjoy coding... */ var x = 0; document.onclick = function( add ) { add = add ? add : window.event; var button = add.target ? add.target : add.srcElement; if (( button.name ) && ( button.name == 'add' )) { x = x +1; _form = document.getElementsByTagName('form')[0]; _text = document.createElement('input'); _button = document.createElement('input'); _div = document.createElement('div'); _div.id = 'div'; _text.type = 'text'; _text.size = '17'; _text.value = 'textBox' + x; _text.id = 'text'; _div.appendChild(_text); _button.type = 'button'; _button.name = 'b1'; _button.id = 'b1'; _button.value = 'Del'; _div.appendChild(_button); _form.appendChild(_div); } if ( button.name && button.name == 'b1' ) { _form.removeChild(document.getElementById('div')); } } //--> </script>
html Syntax (Toggle Plain Text)
</head> <body> <form name="myform"> <input name="txt1" type="text" size="17" /><input name="add" type="button" value="Add" /> </form> </body> </html>
Last edited by essential; Aug 30th, 2008 at 3:01 am.
Dev.Opera — FOLLOW THE STANDARDS, BREAK THE RULES...
Thanks for reminding me!
There are many way's that you can do to disable this function -- depending on how you provide conditional statement's.
Ok in this demo, i will use the x variable as my counter to execute the code for 5x's.
From line #11:
There are many way's that you can do to disable this function -- depending on how you provide conditional statement's.
Ok in this demo, i will use the x variable as my counter to execute the code for 5x's.
From line #11:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
x = x +1; // can also be done as (x += 1) if ( x != 6 || x < 6 ) { /* allowing you to create 5 textfields and after that this function wil be disabled */ } else { return false; } // Just provide only the condition(s) you need on line 11, enclosing all blocks down to line 30. } }
This is exact code u want. Its simple. try this.
html Syntax (Toggle Plain Text)
<html> <head> <title>test</title> <script type="text/javascript"> var counter = 0; function addNew() { // Get the main Div in which all the other divs will be added var mainContainer = document.getElementById('mainContainer'); // Create a new div for holding text and button input elements var newDiv = document.createElement('div'); // Create a new text input var newText = document.createElement('input'); newText.type = "input"; newText.value = counter; // Create a new button input var newDelButton = document.createElement('input'); newDelButton.type = "button"; newDelButton.value = "Delete"; // Append new text input to the newDiv newDiv.appendChild(newText); // Append new button input to the newDiv newDiv.appendChild(newDelButton); // Append newDiv input to the mainContainer div mainContainer.appendChild(newDiv); counter++; // Add a handler to button for deleting the newDiv from the mainContainer newDelButton.onclick = function() { mainContainer.removeChild(newDiv); } } </script> </head> <body > <div id="mainContainer"> <div><input type="text" ><input type="button" value="Add" onClick="addNew()"></div> </div> </body> </html>
Last edited by Luckychap; Mar 4th, 2009 at 2:52 pm.
When you think you have done a lot, then be ready for YOUR downfall.
•
•
Join Date: Mar 2009
Posts: 5
Reputation:
Solved Threads: 0
Hi Shanti,
Thanks for this code. The code is simple and elegant and helped me insert the text boxes inbetween the forums.
I have a question here ?
I created 3 text boxes for each click on the button. I wanted to FOCUS (the cursor) on the second box.
I tried the usual 'document.Myform.Myelement.focus();'
But this is not working, I guess may be due to the fact that the form element is not there yet for sure. Javascript error saying that it is null.
Do you know how to make the cursor placed as well, while we creating the boxes ??
Waiting for your reply.
Thanks
Kalpsen
Thanks for this code. The code is simple and elegant and helped me insert the text boxes inbetween the forums.
I have a question here ?
I created 3 text boxes for each click on the button. I wanted to FOCUS (the cursor) on the second box.
I tried the usual 'document.Myform.Myelement.focus();'
But this is not working, I guess may be due to the fact that the form element is not there yet for sure. Javascript error saying that it is null.
Do you know how to make the cursor placed as well, while we creating the boxes ??
Waiting for your reply.
Thanks
Kalpsen
•
•
Join Date: Apr 2009
Posts: 2
Reputation:
Solved Threads: 0
Hi,
Iam creating a CGI page in that iam creating a text box by using the below code:
But it's now working in the CGI page. But the same working in htm page.
print "<script language=\"javascript\">
function addRow() {
var d=document.getElementById(\"text\");
d.innerHTML+=\"<p><input type='file' value=''name='field[]' />\";
alert (\"hi\");
}
</script>
CGI code:
print $html->input({-id=>$rowid, -type=>'button', -value=>'Add', -onclick=>"addRow()"});
Please let me know if you have any idea about this. Waiting for your reply.
Regards,
Balaji S
Iam creating a CGI page in that iam creating a text box by using the below code:
But it's now working in the CGI page. But the same working in htm page.
print "<script language=\"javascript\">
function addRow() {
var d=document.getElementById(\"text\");
d.innerHTML+=\"<p><input type='file' value=''name='field[]' />\";
alert (\"hi\");
}
</script>
CGI code:
print $html->input({-id=>$rowid, -type=>'button', -value=>'Add', -onclick=>"addRow()"});
Please let me know if you have any idea about this. Waiting for your reply.
Regards,
Balaji S
•
•
•
•
Please feel free to modify this code to match your needs. If you have any question regarding this code you can document.write('Me on my inbox'). lol! Have a good day...
html Syntax (Toggle Plain Text)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Some Title</titles>css Syntax (Toggle Plain Text)
<style type="text/css"> <!-- form div { margin: 4px 4px 0 0; padding: 0; } form input { margin-left: 4px; } form input[type=button] { background-color: #000000; color: silver; margin-left: 2px; } #b1 { background-color: gold; color: black; } --> </style>
javascript Syntax (Toggle Plain Text)
<script type="text/javascript"> <!-- BEGIN HIDING /* A simple demonstration on how to create and delete, textBox with an onclick event. Hope you had great time! Enjoy coding... */ var x = 0; document.onclick = function( add ) { add = add ? add : window.event; var button = add.target ? add.target : add.srcElement; if (( button.name ) && ( button.name == 'add' )) { x = x +1; _form = document.getElementsByTagName('form')[0]; _text = document.createElement('input'); _button = document.createElement('input'); _div = document.createElement('div'); _div.id = 'div'; _text.type = 'text'; _text.size = '17'; _text.value = 'textBox' + x; _text.id = 'text'; _div.appendChild(_text); _button.type = 'button'; _button.name = 'b1'; _button.id = 'b1'; _button.value = 'Del'; _div.appendChild(_button); _form.appendChild(_div); } if ( button.name && button.name == 'b1' ) { _form.removeChild(document.getElementById('div')); } } //--> </script>
html Syntax (Toggle Plain Text)
</head> <body> <form name="myform"> <input name="txt1" type="text" size="17" /><input name="add" type="button" value="Add" /> </form> </body> </html>
Try this format:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
print '<script type="text/javascript"> function addRow() { var d = document.getElementById("text"); d.innerHTML += \'<p><input type="file" value="" name="field[]" /></p>\'; alert("Hi!"); } </script>';
![]() |
Similar Threads
- about registry key . . . (Visual Basic 4 / 5 / 6)
- Please help me in this coding (Java)
- How to split a large list (Pascal and Delphi)
- Need help with php mailing list/redirect (PHP)
- Select Box populates text field (JavaScript / DHTML / AJAX)
- Extracting text from a multi-line Edit Box (win32api using c++) (C++)
- ASP add, update and delete (ASP)
- Add textbox to editable div problem (JavaScript / DHTML / AJAX)
- beans bound property: getNewValue() doesn't work out. Code attached (Java)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: browser loads the json after double clicking the find text button
- Next Thread: New to javascript question
Views: 8189 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for JavaScript / DHTML / AJAX
acid2 ajax ajaxcode ajaxexample ajaxhelp animate array automatically autoplay beta boarder box button captcha card cart codes column css date debugger decimal design developer dom download element embed enter error events firefox firehose flash focus form frameworks getselection google gwt hint html htmlform ie7 iframe image() index java javascript javascripthelp2020 javascripts jawascriptruntimeerror jquery jsp listbox maps marquee masterpage menu microsoft mimic mp4 offline onmouseover parameters paypal php player position post problem programming prototype rating redirect regex safari scale scriptlets search select size sources sql starrating textarea toggle tweet twitter validation variables w3c web webkit webservice website window windowofwords xml xspf






