Reputation: 11
I would like to know if there is anyway for a user to add to the elements of a html5 webpage? For example, is there anyway that they can add a paragraph or image to an existing webpage that wasn't there before?
eg.
<!doctype html>
<html>
<head>
</head>
<body>
<p class=”pClass”>This is in a paragraph.</p>
**//would it be possible for a user to insert several paragraphs here with the click of a button or something?**
</body>
</html>
Upvotes: 1
Views: 629
Reputation: 50905
I probably went a little too far with making this, but I always like creating these little examples. Here's what I came up with. Start with this HTML:
<input type="text" id="txt1" />
<button id="btn1">Add Item</button>
<hr />
<div id="targetArea"></div>
And this CSS:
p.editable input.editor,
p.editable input.action,
p.editable em.escape {
display: none;
}
p.editable input.action {
margin-left: 15px;
}
p.editable em.escape {
margin-left: 10px;
font-size: 8px;
}
p.editable:hover input.action {
display: inline;
}
p.editable.editing span.text {
display: none !important;
}
p.editable.editing input.editor,
p.editable.editing input.action,
p.editable.editing em.escape {
display: inline !important;
}
And use this JavaScript:
var textProp = "textContent" in document.createElement("div") ? "textContent" : "innerText";
function strTrim(str) {
if (!str) {
str = "";
}
return str.replace(/^\s+|\s+$/g, "");
}
function addEvent(element, eventName, callback) {
if (element.addEventListener) {
element.addEventListener(eventName, callback, false);
} else if (element.attachEvent) {
element.attachEvent("on" + eventName, callback);
}
}
function editableClickHandler(e) {
var actionButton, pNode, myText, myEditor;
actionButton = this;
pNode = actionButton.parentNode;
myText = pNode.querySelector("span.text");
myEditor = pNode.querySelector("input.editor");
if (actionButton.value === "Edit") {
actionButton.value = "Done";
pNode.className += " editing";
myEditor.setAttribute("data-original-val", myText[textProp]); // Save current value in case of canceling
myEditor.value = myText[textProp];
} else {
actionButton.value = "Edit";
pNode.className = "editable";
myText[textProp] = myEditor.value;
}
}
function escapeCheckHandler(e) {
var keyCode, pNode, myEditor, myText, myActionButton;
e = e || window.event; // Normalize event
keyCode = e.keyCode || e.which || e.charCode; // Normalize keycode
if (keyCode === 27) { // Escape key
pNode = this.parentNode;
myEditor = pNode.querySelector("input.editor");
myText = pNode.querySelector("span.text");
myActionButton = pNode.querySelector("input.action");
pNode.className = "editable";
myText = myEditor.getAttribute("data-original-val");
myActionButton.value = "Edit";
}
}
function addClickHandler() {
var target, curInput, curInputVal, newP, newText, newEditor, newActionButton, newEscapeInfo;
curInput = document.getElementById("txt1");
curInputVal = strTrim(curInput.value);
if (!curInputVal) {
alert("Must provide actual text");
return;
}
target = document.getElementById("targetArea");
newP = document.createElement("p");
newText = document.createElement("span");
newEditor = document.createElement("input");
newActionButton = document.createElement("input");
newP.className = "editable";
newText.className = "text";
newText[textProp] = curInputVal;
newP.appendChild(newText);
newEditor = document.createElement("input");
newEditor.type = "text";
newEditor.className = "editor";
addEvent(newEditor, "keyup", escapeCheckHandler);
newP.appendChild(newEditor);
newActionButton.type = "button";
newActionButton.className = "action";
newActionButton.value = "Edit";
addEvent(newActionButton, "click", editableClickHandler);
newP.appendChild(newActionButton);
newEscapeInfo = document.createElement("em");
newEscapeInfo.className = "escape";
newEscapeInfo[textProp] = "(Press Escape to Cancel Editing)";
newP.appendChild(newEscapeInfo);
curInput.value = "";
target.insertBefore(newP, target.firstChild);
}
function loadHandler() {
addEvent(document.getElementById("btn1"), "click", addClickHandler);
}
addEvent(window, "load", loadHandler);
DEMO: http://jsfiddle.net/8K3pN/2/
The use of addEvent
helps to bind event handlers more consistently across browsers.
What happens overall, is when you fill in the textbox and click the button, it adds a <p>
(with some child elements) to the target area. Depending on the state of the p
, certain things are hidden/shown (with the CSS). And depending on the state of the button is clicked (Edit/Done), certain things happen. I added in the ability to press the Escape key to "cancel" editing (instead of adding another button).
Again, I know this was probably too much, but I hope this helps you understand how this all can work together!
Upvotes: 2
Reputation: 11
is this what you had in mind?
<!doctype html>
<html>
<head>
<script type="text/javascript">
var count = 1;
function newPara() {
var new_p = document.createElement('p');
new_p.setAttribute('class','pClass');
new_p.setAttribute('id','new_para_'+ count++);
var newContent = document.createTextNode(document.getElementById('content').value);
new_p.appendChild(newContent);
document.getElementsByTagName('body')[0].insertBefore(new_p,document.getElementById('create'));
}
</script>
</head>
<body>
<p class=”pClass”>This is in a paragraph. <br/></p>
<div id="create">
<textarea id="content"> </textarea>
<button onclick="newPara();"> Create </button>
</div>
</body>
</html>
Upvotes: 0
Reputation: 2115
AS the others have said we're not sure what you want to accomplish, but you can add elements to a document from JavaScript using the document.createElement()
function. You could provide a text box to enter the text and then call a JS function add a paragraph to to your div.
Upvotes: 0