?
Solved

html, javascript

Posted on 2008-10-22
11
Medium Priority
?
391 Views
Last Modified: 2010-04-21
I have a html form.....
I want to hide one set of data from the user and on click of "Click me" i want to display a text box , a drop down box and a delete button to delete the complete div.

any help appreciated for both display and delete.
0
Comment
Question by:sakvk
[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
  • 6
  • 4
11 Comments
 
LVL 8

Expert Comment

by:MatthiasVance
ID: 22774995
Here's some example code, I hope it helps you.

Kind regards,

Matthias Vance
<script language="JavaScript">
	function showForm(show) {
		document.getElementById("form").style.display = show ? "block" : "none";
	}
</script>
<button onclick="showForm(true);">Show</button><br />
<button onclick="showForm(false);">Hide</button><br />
<form id="form" style="display: none;">
	<input type="text" /><br />
	<select>
		<option value="1">One</option>
		<option value="2">Two</option>
		<option value="3">Three</option>
	</select>
</form>

Open in new window

0
 
LVL 9

Expert Comment

by:Shahzad Fateh Ali
ID: 22775008
Hi,

Create <div> for each set of data.
To display use
document.getElementById('divID').style.display = 'block';

To hide use
document.getElementById('divID').style.display = 'none';
0
 

Author Comment

by:sakvk
ID: 22775366
Hey MatthiasVance,

Thanks for your reply...yes.. its very similar.. but everytime i click on the button, it should show me the text box and the drop down box....
So if u click on the show button 3 times, I will have 3 times (text box and drop down box) on my form.
now its showing only once....
0
Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

 
LVL 8

Expert Comment

by:MatthiasVance
ID: 22775394
So you basically want to add and remove elements from a form? This requires a somewhat different approach. I will write some example code in a few moments.

Kind regards,

Matthias Vance
0
 
LVL 8

Expert Comment

by:MatthiasVance
ID: 22775574
Here is some example code. Note that the naming of the entries doesn't function correct when you remove an entry from the middle. This can easily be fixed by using one button at the bottom of the form that only deletes the last entry, or otherwise. If you want the code to do that, please say so.

Kind regards,

Matthias Vance
<script language="JavaScript">
	function deleteEntry(element) {
		document.getElementById("form").removeChild(element.parentNode);
	}
 
	function addEntry() {
		// Initialize
		var form = document.getElementById("form");
		var add = document.getElementById("add");
		var count = form.getElementsByTagName("div").length;
		
		// Create new div
		var newDiv = document.createElement("div");
		
		// Create text box
		var newInput = document.createElement("input");
		newInput.setAttribute("type", "text");
		newInput.setAttribute("name", "text[" + count + "]");
		newDiv.appendChild(newInput);
		
		// Create dropdown box
		var newSelect= document.createElement("select");
		newSelect.setAttribute("name", "text[" + count + "]");
		newDiv.appendChild(newSelect);
		
		// Create delete button
		var newButton = document.createElement("button");
		newButton.appendChild(document.createTextNode("Delete"));
		newButton.setAttribute("onclick", "deleteEntry(this);");
		newDiv.appendChild(newButton);
		
		form.insertBefore(newDiv, add);
	}
</script>
<form id="form" onsubmit="return false;">
<div>
	<input type="text" name="text[0]" /><select name="select[0]"></select><button onclick="deleteEntry(this);">Delete</button>
</div>
<button id="add" onclick="addEntry();">Add entry</button>
<input type="submit" value="Submit" />
</form>

Open in new window

0
 

Author Comment

by:sakvk
ID: 22775661
Hey MattiasVance,

you are really great.... just too fast...

2 questions for you
1. Can i replace the addEntry button with a hyperlink like "Add Entry" and will it work the same way?
2. Can the deletion in the middle be made possible? as the user might need to delete in the middle after entering the whole set of values?
please do let me know


0
 
LVL 8

Expert Comment

by:MatthiasVance
ID: 22775738
I'll update the code and get back to you in a few moments.
0
 
LVL 8

Accepted Solution

by:
MatthiasVance earned 2000 total points
ID: 22775863
I updated the example code.

Kind regards,

Matthias Vance
<script language="JavaScript">
	function deleteEntry(element) {
		document.getElementById("form").removeChild(element.parentNode);
	}
 
	function getAvailableId() {
		var form = document.getElementById("form");
		var count = form.getElementsByTagName("div").length;
	}
 
	function addEntry() {
		// Initialize
		var form = document.getElementById("form");
		var divs = form.getElementsByTagName("div");
		
		// Auto increment names
		var name = divs[divs.length - 1].getElementsByTagName("input")[0].getAttribute("name");
		name = name.substring(5, name.length - 1);
		var newName = parseInt(name) + 1;
		
		// Create new div
		var newDiv = document.createElement("div");
		
		// Create text box
		var newInput = document.createElement("input");
		newInput.setAttribute("type", "text");
		newInput.setAttribute("name", "text[" + newName + "]");
		newDiv.appendChild(newInput);
		
		// Create dropdown box
		var newSelect= document.createElement("select");
		newSelect.setAttribute("name", "text[" + newName + "]");
		newDiv.appendChild(newSelect);
		
		// Create delete button
		var newButton = document.createElement("button");
		newButton.appendChild(document.createTextNode("Delete"));
		newButton.setAttribute("onclick", "deleteEntry(this);");
		newDiv.appendChild(newButton);
		
		form.insertBefore(newDiv, document.getElementById("add"));
	}
</script>
<form id="form" onsubmit="return false;">
<div>
	<input type="text" name="text[0]" /><select name="select[0]"></select><button onclick="deleteEntry(this);">Delete</button>
</div>
<!-- you could add a href="#" to this to make it look like a real link -->
<a id="add" onclick="addEntry();">Add entry</a>
<input type="submit" value="Submit" />
</form>

Open in new window

0
 

Author Comment

by:sakvk
ID: 22775932
i think it is still deleting the last line item every time..... if you could help me fix that it would be great.... but i will still assign all the points to you.

I have another question, on similar lines.... hope you can help me with that tooo.
0
 

Author Closing Comment

by:sakvk
ID: 31508677
thanks a ton  for your quick help....
0
 
LVL 8

Expert Comment

by:MatthiasVance
ID: 22775957
Actually, it's not removing the last line. Try to write some text in the first and last boxes if you have three, and delete the middle.

Kind regards,

Matthias Vance
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
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…
Suggested Courses

752 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