473,406 Members | 2,345 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Cannot remove a dynamically added text field.

57
Hi all

I have a pretty cool script which, on the click of a button, sticks in a extra text box and gives it a unique name/id so it can be munipulated and sent through on a form.
Problem is, I dont know how to remove a text box when added, simplistic terms - a script to reverse what the add button does.

Here's a simple break down of the code...

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. <!--
  3. function addField(area,field,limit) {
  4.  
  5.     var field_area = document.getElementById(area);
  6.     var all_inputs = field_area.getElementsByTagName("input"); 
  7.  
  8.     var last_item = all_inputs.length - 1;
  9.     var last = all_inputs[last_item].id;
  10.     var count = Number(last.split("_")[1]) + 1;
  11.  
  12.     if(count > limit && limit > 0) return;
  13.  
  14.  if(document.createElement) {
  15.   var li = document.createElement("li");
  16.   var input = document.createElement("input");
  17.   input.id = field+count;
  18.   input.name = field+count;
  19.   input.type = "text"; text,file,checkbox etc.
  20.   li.appendChild(input);
  21.   field_area.appendChild(li);
  22.  } else { //Older Method
  23.   field_area.innerHTML += "<li><input name='"+(field+count)+"' id='"+(field+count)+"' type='text' /></li>";
  24.  }
  25. }
  26.  
  27. //-->
  28. </script>


[HTML]</head>
<body>
<form name="frm" method="POST">
<strong>Friends List</strong><br />
<ol id="friends_area">
<li><input type="text" name="friend_1" id="friend_1" /></li>
<li><input type="text" name="friend_2" id="friend_2" /></li>
<li><input type="text" name="friend_3" id="friend_3" /></li>
<li><input type="text" name="friend_4" id="friend_4" /></li>
<li><input type="text" name="friend_5" id="friend_5" /></li>
</ol>
<input type="button" value="Add Friend Field" onclick="addField('friends_area','friend_',15);" />
</form>

</body>
</html>[/HTML]

Any suggestions how to remove a field????

Regards
May 12 '08 #1
9 2351
Logician
210 100+
I wonder if you have you tested this code with I.E., which has a problem creating input fields in the conventional way? If adding to existing fileds, it would be better to use .cloneNode.
The innerHTML branch is pointless since it doesn't represent an 'older method' and if document.createElement isn't supported, neither will innerHTML .

Expand|Select|Wrap|Line Numbers
  1.   input.type = "text"; text,file,checkbox etc.
  2.  
That must cause an error.

If you append a property to field_area, holding either a reference to the last field created or its ID, it would be easy for a removal function to identify it.
May 12 '08 #2
hsriat
1,654 Expert 1GB
Try this...
Expand|Select|Wrap|Line Numbers
  1. function removeField(listId) {
  2.     var toBeRemoved = document.getElementById(listId).lastChild;
  3.     toBeRemoved.parentNode.removeChild(toBeRemoved);
  4. }
[html]<input type="button" value="Remove Friend Field" onclick="removeField('friends_area');" />[/html]
May 12 '08 #3
plumba
57
I wonder if you have you tested this code with I.E., which has a problem creating input fields in the conventional way? If adding to existing fileds, it would be better to use .cloneNode.
The innerHTML branch is pointless since it doesn't represent an 'older method' and if document.createElement isn't supported, neither will innerHTML .

That must cause an error.

If you append a property to field_area, holding either a reference to the last field created or its ID, it would be easy for a removal function to identify it.
You are correct.. l removed the // just before the word text - only in my post though.
May 13 '08 #4
plumba
57
Try this...
Expand|Select|Wrap|Line Numbers
  1. function removeField(listId) {
  2.     var toBeRemoved = document.getElementById(listId).lastChild;
  3.     toBeRemoved.parentNode.removeChild(toBeRemoved);
  4. }
[html]<input type="button" value="Remove Friend Field" onclick="removeField('friends_area');" />[/html]

Brilliant!! This works a treat!!! Is was throwing up an 'object required' error for a bit - until I figured out the typo [HTML]onclick="removeField('friends_area');" />[/HTML] it did not require the 's' in friends_area.

One small problem though, this will actually allow the user to remove all fields, I would like it to leave at least one field on there - otherwise after deleting all fields it will not allow you to add a field again.

Thanks for your help!!
May 13 '08 #5
hsriat
1,654 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. function removeField(listId) {
  2.     var toBeRemoved = document.getElementById(listId).lastChild;
  3.     if (!toBeRemoved.previousSibling) return;
  4.     toBeRemoved.parentNode.removeChild(toBeRemoved);
  5. }
it did not require the 's' in friends_area.
Could not understand the reason...
May 13 '08 #6
plumba
57
Expand|Select|Wrap|Line Numbers
  1. function removeField(listId) {
  2.     var toBeRemoved = document.getElementById(listId).lastChild;
  3.     if (!toBeRemoved.previousSibling) return;
  4.     toBeRemoved.parentNode.removeChild(toBeRemoved);
  5. }

Could not understand the reason...
That works a treat. Many thanks for your speedy and reliable help hsriat.

Kind regrds
May 13 '08 #7
hsriat
1,654 Expert 1GB
That works a treat. Many thanks for your speedy and reliable help hsriat.

Kind regrds
You are welcome


Regards,
Harpreet
May 13 '08 #8
plumba
57
You are welcome


Regards,
Harpreet

Ok, it's me again....

I just want to enhance this slightly, and I think this may be quite simple.

I want to add some span tags to the end of each line, as below:

[HTML]<ol id="friend_area">
<li><input type="text" name="friend_1" id="friend_1" /></li><span id ="1"></span>
<li><input type="text" name="friend_2" id="friend_2" /></li><span id ="2"></span>
<li><input type="text" name="friend_3" id="friend_3" /></li><span id ="3"></span>
<li><input type="text" name="friend_4" id="friend_4" /></li><span id ="4"></span>
<li><input type="text" name="friend_5" id="friend_5" /></li><span id ="5"></span>
</ol>[/HTML]

Is it easy enough to get the function which adds a line to add a span tag to each new line - I'm guessing the number is already generated in the code so it's just a case of adding the span and number into the
Expand|Select|Wrap|Line Numbers
  1. field_area.innerHTML += "<li><input name='"+(field+count)+"' id='"+(field+count)+"' type='text' /></li>";
line??
May 13 '08 #9
hsriat
1,654 Expert 1GB
Don't keep the span tag outside li. Keep it within the li instead.
Expand|Select|Wrap|Line Numbers
  1. field_area.innerHTML += '<li><input name="'+(field+count)+'" id="'+(field+count)+'" type="text" /><span id="'+count+'"></span></li>';
May 13 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Ken | last post by:
I am trying to create one image using JavaScript; then later in the script remove the image - not just remove the src. The following creates the image, but I have been unable to remove it. How...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
6
by: Stu Carter | last post by:
Hi, I have an aspx page where some controls are initially disabled by the code-behind 'Page_Load' event. I want these controls to be dynamically enabled when the user checks a checkbox. ...
2
by: djc | last post by:
On the page_load event I am querying a database and binding data to some text boxes, list boxes, and a repeater control. When the page loads it uses the value of one of the database fields (status)...
7
by: Samuel | last post by:
Hi, I am building a page that makes use of user control as a templating technique. The following is that I have in mind and it is actually working: Root/ -- login.aspx -- login.aspx.vb --...
6
by: Aaron Smith | last post by:
Ok. I have a dataset that has multiple tables in it. In one of the child tables, I have a column that I added to the DataSet (Not in the DataSource). This column does not need to be stored in the...
5
by: stellstarin | last post by:
I have a html where fields are created and added dynamically on the client side. I use the AppendChild() call to create fields dynamically. On submit i try to get the value for all the...
2
by: Tereska | last post by:
I want to delete script added before. I'm adding script dynamically and i'm removing later. Why it is still working? I have something like this: <html> <head> <title>JS Script...
2
by: karinmorena | last post by:
I'm having 4 errors, I'm very new at this and I would appreciate your input. The error I get is: Week5MortgageGUI.java:151:cannot find symbol symbol: method allInterest(double,double,double)...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.