Here is the UI scenario I'm trying to develop: There is a text box field below which there are two buttons. On clicking the first button , there will be a popup in which the user will have to add a IP address. On pressing submit on the popup , the IP address will be displayed in the text area. The user can keep on adding IP addresses, which will be visible on the text area in different lines. The user should be able to select a particular IP address from the box and delete it using the second button. The user should not be able to enter data directly into the text field. Any ideas ?
1 Answer
I would suggest you keep clear of using a <textarea> since this involves sniffing selections etc. Instead, use simple container elements (e.g., <li>s in a <ul>/<ol> cf. @David Thomas' comment) to hold the entered IP addresses (one each).
I trust you'll get the idea from this JsFiddle.
HTML
<ul id="addresses"></ul>
<hr />
<a href="#" id="add">Add...</a>
JS (Assuming jQuery. You would need some framework to handle selections too.)
var deleteHandler = function() {
$(this).parent().remove();
}
$("#add").click(function() {
var ip = prompt("Enter IP:");
if (ip) {
var $deleteIp = $("<a />", {"class": "delete"})
.text("X")
.click(deleteHandler);
$("<li />")
.text(ip)
.append($deleteIp)
.appendTo("#addresses");
}
return false;
});
Just a demo — deemed to be a candidate for improvements :)
ulorolbe better suited?<select multiple>since he wants to be able to select an IP from the list later.